Problem with MVC and DataView

Last post 02-03-2008 11:00 AM by rjcox. 4 replies.

Sort Posts:

  • Problem with MVC and DataView

    02-01-2008, 6:51 PM
    • Member
      point Member
    • stephen_wood
    • Member since 02-01-2008, 11:49 PM
    • Posts 2

    I'm having a problem when I place a DataView on a View-derived page.  I am unable to reference the DataView in the code-behind for the page.

    Anyone know why?

    Thanks,

    Stephen
     

  • Re: Problem with MVC and DataView

    02-02-2008, 2:35 AM
    Answer
    • All-Star
      29,644 point All-Star
    • Fredrik N
    • Member since 06-22-2002, 5:03 AM
    • Sweden
    • Posts 5,334
    • TrustedFriends-MVPs

    When you refer to DataView, is it the DetailsView data-bound control, or is it the ViewData (the data you pass to the RenderView method that holds the model to render)? (I assume you aren't taling about the DataView object within the System.Data namespace!?).

    If it's the DetailsView contorl you are talking about, you can simply get access to it by declare a variable in the code-behind with the same name as the ID of the DetailsView control:

    protected System.Web.UI.WebControls.DetailsView DetailsView1;

    public void Page_Load()
    {
        DetailsView1.DataSource = this.ViewData;
        DetailsView1.DataBind();
    }

    I hope you are aware of that the MVC Framework don't work with postback and ViewState (not 100% true). The idea is to use Controllers that handle the user input and "events", by using Action methods which takes the user input etc. I think you already know this and will only use the DetailsView to render data, nothing more (if you refer to the DetailsView when you say DataView).

     

    If you want to get access to the model you pass to the RenderView method or added to the Controller's ViewData property, you simply use the ViewData property of the ViewPage (The base class a page inherit).

    /Fredrik Normén - fredrikn @ twitter

    ASPInsider

    Microsoft MVP, MCSD, MCAD, MCT

    ASPInsiders
    My Blog
  • Re: Problem with MVC and DataView

    02-02-2008, 3:01 AM
    Answer

    The issue here is the template used by the Create MVC View command in VS doesn't have a .designer file. You don't need to declare these things manually in your code behind. Just right click your page in your solution explorer, and choose Convert to Web Application to automatically generate a .designer file. Or download an updated template:

    http://www.squaredroot.com/post/2008/01/MVC-Template-Fix.aspx 

    This should be fixed with the next drop. Scott Guthrie posted about it on his blog, but I couldn't find it searching quick just now.

  • Re: Problem with MVC and DataView

    02-02-2008, 1:21 PM
    • Member
      point Member
    • stephen_wood
    • Member since 02-01-2008, 11:49 PM
    • Posts 2

    Thanks.  Good answers all.

     Regards,

    Stephen
     

  • Re: Problem with MVC and DataView

    02-03-2008, 11:00 AM
    • Contributor
      7,054 point Contributor
    • rjcox
    • Member since 12-19-2007, 9:14 AM
    • Basingstoke, UK
    • Posts 1,444

    sliderhouserules:
    Scott Guthrie posted about it on his blog, but I couldn't find it searching quick just now.
     

    It is hidden down in the large number of comments. 

    Richard
Page 1 of 1 (5 items)