[ACCEPTED]-asp.net MVC RC1 RenderPartial ViewDataDictionary-asp.net-mvc

Accepted answer
Score: 35

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

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

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

Score: 13

Don't know if anyone still cares but I used 5 a KeyValuePair for the ViewDataDictionary.

 <% Html.RenderPartial("ItemRow", item, new ViewDataDictionary{
        new KeyValuePair<string, object>("url", url),
        new KeyValuePair<string, object>("count", count),
        new KeyValuePair<string, object>("className", className)
 }); %>

This 4 way you can write everything in one statement. The 3 KVPs can be accessed in the view by:

<%= ViewData["url"] %>
<%= ViewData["count"] %>
<%= ViewData["className"] %>

While 2 what I passed through the model can be accessed 1 through Model.*

Score: 6

I'm using the RTM version of ASP.Net MVC, but 4 with:

<% Html.RenderPartial("~/Views/Project/Projects.ascx", new ViewDataDictionary(new {Key = "Some value"})); %>

Try the following when referencing 3 the value in your partial view:

<%= ViewData.Eval("Key") %>

That seems 2 to have done the trick for me. It will also 1 work if you just do this:

<% Html.RenderPartial("~/Views/Project/Projects.ascx", new {Key = "Some value"}); %>

Hope this helps.

Score: 5

Have you tried:

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

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


Score: 1

In your main view:

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

Either in you controller 3 or your main view:

ViewData["Test"] = "Mark";

If you don't specify a 2 model or view data dictionary in RenderPartail, it 1 uses the ones from the containing view.

More Related questions