Passing ViewData to MVC User Controls

Last post 12-17-2007 11:46 PM by abombss. 2 replies.

Sort Posts:

  • Passing ViewData to MVC User Controls

    12-17-2007, 8:17 PM
    • Loading...
    • f00sion
    • Joined on 05-28-2004, 5:58 PM
    • Irvine, CA
    • Posts 48

    I haven't been able to find any posts or documentation about how to do this and can't seem to get it working on my own.  In my controller I am adding two lists to the viewdata like this:

    ViewData["list1"] = mylist;
    ViewData["list2"] = myotherlist;

    I have created a control that inherits from mvc.viewusercontrol, the control just writes out the count of items from the list, nothing special.
    On the view i have placed two instances of this control and set the ViewDataKeyName properties to "list1" and "list2" respectively.

    When running, the control throws an exception that list1 can not be found in the viewdata dictionary.  If I leave off the ViewDataKeyName property from the control declaration then I have access to the full viewdata dictionary and can clearly see that both keys do exist.  Has anyone else tried the user controls yet?.. Maybe i'm going about it all wrong.  For now I just set up another property on my control that reads the proper list from viewdata, but that is certainly not as slick.

    Filed under: , ,
  • Re: Passing ViewData to MVC User Controls

    12-17-2007, 11:35 PM
    • Loading...
    • jimzim
    • Joined on 02-03-2006, 2:31 PM
    • Tampa, FL
    • Posts 40
    • TrustedFriends-MVPs

     Here is a good post from ScottGu on how he show you how to add objects into the ViewData dictionary using a key/value pattern like you are trying above.

    http://weblogs.asp.net/scottgu/archive/2007/12/06/asp-net-mvc-framework-part-3-passing-viewdata-from-controllers-to-views.aspx

     Now he is using a Page instead of a user control, but it should work the same way.  Also try your code in a Page also to see if it is the control or not.

     

     

    Jim Zimmerman
    Filed under: ,
  • Re: Passing ViewData to MVC User Controls

    12-17-2007, 11:46 PM
    • Loading...
    • abombss
    • Joined on 06-27-2006, 12:13 PM
    • Chicago, IL
    • Posts 164

    This is a bug.  Nice catch.

    It works if you have a strongly typed ViewData or if your ViewData is an anonymous type.

    I was able to do <%=ViewData["Count"]%> when I used List1 as the ViewDataKey and I used an anonymous type for my view data.

    It uses a TypeDescriptor to get the properties off the view data and when you are using a regular dictionary the keys are not properties so it throws.

    I personally don't know if I like this anyway, its weird if you pass in an IEnumerable because you do not get direct access to the collection, only its properties.  So if do this

    <uc1:ListPeopleControl runat="server" DataViewKey="People" />

    <% foreach(Person person in this.ViewData) { %> <%=person.Name%> <% } %>

    The above does not work as ViewData cannot be cast to my IEnumerable<Person> so to make this work I need a container for my collection:

    new {List1=new {RealList=new List<Person>()}}. 

    Now I can pass my ViewDataKey "List1" and access it via the ViewData property ViewData["RealList"]

    Am I am missing something?

     

    BTW -- Bug fix is below

    Change the following line in in EnsureViewData of ViewUserControl
     

    else
    {
      _viewData = DataBinder.Eval(viewDataContainer.ViewData, ViewDataKey);
    }
    to
    else
    {
        IDictionary dictionary = viewDataContainer.ViewData as IDictionary;
    if (dictionary != null)
    {
    _viewData = dictionary[ViewDataKey];
    }
    else { _viewData = DataBinder.Eval(viewDataContainer.ViewData, ViewDataKey); } }
    Adam Tybor -- abombss.com
    Filed under: , , ,
Page 1 of 1 (3 items)
Microsoft Communities
Page view counter