23

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...

Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
Mark79
  • 233
  • 1
  • 2
  • 7

5 Answers5

36

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.

Alan
  • 361
  • 3
  • 2
13

Don't know if anyone still cares but I used 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 way you can write everything in one statement. The KVPs can be accessed in the view by:

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

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

Josiah Ruddell
  • 29,697
  • 8
  • 65
  • 67
6

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

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

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

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

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

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

Hope this helps.

5

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

Nick Berardi
  • 54,393
  • 15
  • 113
  • 135
  • Thanks but I'm able to pass the 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.. – Mark79 Jan 30 '09 at 13:09
1

In your main view:

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

Either in you controller or your main view:

ViewData["Test"] = "Mark";

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

Tim Scott
  • 15,106
  • 9
  • 65
  • 79