views:

3275

answers:

6

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 stronlgy 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  A: 

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
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
A: 

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
A: 

Thanks Nick. I did as you advised in the end and it works like that but i'm still interested in knowing how to pass an "object model" into the ViewDataDictionary overload.

like this...

<% Html.RenderPartial("~/Views/Project/Projects.ascx", null, new ViewDataDictionary(object model)); %>

i can do this with a ViewDataDictionary object like this...

<% ViewDataDictionary dictionary = new ViewDataDictionary();
dictionary.Add("Test", "Mark");

Html.RenderPartial("~/Views/Project/Projects.ascx", null, new ViewDataDictionary(dictionary)); %>

and then in my view user control i can access the dictionary like expected:

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

Thanks

Mark79
This should be in a comment or as an edit to your question. Not an answer.
Geoffrey Chetwood
I was wondering of you got this to work the way you had intended. I too am facing the same problem.
Picflight
+4  A: 

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.

+3  A: 

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
+3  A: 

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