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:
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.
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.
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.
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
f00sion
Member
192 Points
79 Posts
Passing ViewData to MVC User Controls
Dec 18, 2007 12:17 AM|LINK
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.
MVC viewdata user control
jimzim
Member
243 Points
41 Posts
Re: Passing ViewData to MVC User Controls
Dec 18, 2007 03:35 AM|LINK
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.
MVC viewdata
abombss
Member
575 Points
164 Posts
Re: Passing ViewData to MVC User Controls
Dec 18, 2007 03:46 AM|LINK
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); }toASP.NET MVC MVC workaround bug