asp.net MVC RC1 RenderPartial ViewDataDictionary


Question

I'm trying to pass a ViewData object from a master page to a view user control using the ViewDataDictionary.

The problem is the ViewDataDictionary is not returning any values in the view user control whichever way I try it.

The sample code below is using an anonymous object just for demonstration although neither this method or passing a ViewData object works.

Following is the RenderPartial helper method I'm trying to use:

<% Html.RenderPartial("/Views/Project/Projects.ascx", ViewData.Eval("Projects"), new ViewDataDictionary(new { Test = "Mark" })); %>

and in my view user control i do the following:

<%= Html.Encode(ViewData["Test"]) %>

Why does this not return anything?

Thanks for your help.

EDIT:

I'm able to pass and access the strongly typed model without any problems. it's the ViewDataDictionary which I'm trying to use to pass say just a single value outside of the model...

1
22
4/14/2013 3:19:59 PM

Accepted Answer

Have you tried:

<% Html.RenderPartial("~/Views/Project/Projects.ascx", ViewData); %>

Also have you verified ViewData["Test"] is in the ViewData before you are passing it? Also note that when passing your ViewData to a Partial Control that it is important to keep the Model the same.

Nick

5
1/30/2009 12:54:14 PM

This is the neatest way I've seen to do this:

<% Html.RenderPartial("/Views/Project/Projects.ascx", Model, new ViewDataDictionary{{"key","value"}});%>

It may be a little hackish, but it let's you send the model through AND some extra data.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon