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

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

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

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.