37

Is there a way to pass a piece of extra data along with a model to a Partial view?

E.G.

@Html.Partial("_SomeTable", (List<CustomTable>)ViewBag.Table);

Is what I have now. Can I add something else without changing my Model?

@Html.Partial("_SomeTable", (List<CustomTable>)ViewBag.Table, "TemporaryTable");

I see ViewDataDictionary as a param. I am not sure what this object does or if this meets my need.

P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348

4 Answers4

66

ViewDataDictionary can be used to replace the ViewData dictionary in the partial view... If you don't pass a ViewDataDictionary parameter then the parial's viewdata is the same as the parents.

An example of how to use it in the parent is:

@Html.Partial("_SomeTable", (List<CustomTable>)ViewBag.Table, new ViewDataDictionary {{ "Key", obj }});

Then within the partial you can access this obj as follows:

@{ var obj = ViewData["key"]; }

A completely different approach woud be to use the Tuple class to group both the original model and extra data together as follows:

@Html.Partial("_SomeTable", Tuple.Create<List<CustomTable>, string>((List<CustomTable>)ViewBag.Table, "Extra data"));

The model type for the partial would then be:

@model Tuple<List<CustomTable>, string>

Model.Item1 gives the List object and Model.Item2 gives the string

Martin Booth
  • 8,485
  • 31
  • 31
  • 2
    I can't understand the syntax in `new ViewDataDictionary {{ "Key", obj }}`. Where are the parentheses () in `new ViewDataDictionary()` – Mahmood Dehghan Nov 25 '12 at 15:12
  • 2
    They are redundant when you have an object initializer.. which is what the code is directly after new ViewDataDictionary.. see http://msdn.microsoft.com/en-us/library/bb531208.aspx – Martin Booth Nov 29 '12 at 02:35
  • 1
    `@{ var obj = ViewBag.Key; }` also works to access the passed data – MEC Aug 16 '15 at 11:21
7

You should be able to just put it in the ViewBag and then access it from the ViewBag in the partial view. See this SO answer.

Community
  • 1
  • 1
Tridus
  • 5,021
  • 1
  • 19
  • 19
6

I came across this issue as well. I wanted to have a snippet of code reproduced many times, and did not want to copy paste. The code would vary slightly. After looking at the other answers, I did not want to go that exact route and decided instead to just use a plain Dictionary.

For example:

parent.cshtml

@{
 var args = new Dictionary<string,string>();
 args["redirectController"] = "Admin";
 args["redirectAction"] = "User";
}
@Html.Partial("_childPartial",args)

_childPartial.cshtml

@model Dictionary<string,string>
<div>@Model["redirectController"]</div>
<div>@Model["redirectAction"]</div>
Travis J
  • 81,153
  • 41
  • 202
  • 273
3

You can get even clever as shown here by Craig Stuntz

Html.RenderPartial("SomePartialView", null, 
    new ViewDataDictionary(new ViewDataDictionary() { {"SomeDisplayParameter", true }})
        { Model = MyModelObject });
Community
  • 1
  • 1
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689