DaveDev
DaveDev

Reputation: 42205

Can somebody please correct my understanding of how ViewDataDictionary works?

Can somebody correct my understanding of what's going on here?

Suppose the following:

<% foreach (var company in Model.Companies) { %>

    <% Html.RenderPartial("FundList", Model, new ViewDataDictionary(company)); %>

<% } %>

I was under the impression that this will render a Partial View called FundList.ascx, while passing a Model object (which contains a load of stuff), and also a company object, which contains data specific to a company.

However, when I inspect the data available to me in FundList, I can only see references to the original Model object. I don't see company anywhere. Should this be available in ViewData?

How would it be best for me to get a company object in the FundData partial?

Upvotes: 2

Views: 110

Answers (3)

fearofawhackplanet
fearofawhackplanet

Reputation: 53446

Did you try...

   <% Html.RenderPartial(
          "FundList", 
          Model, 
          new ViewDataDictionary { { "company", company } }); %>

Upvotes: 2

benwasd
benwasd

Reputation: 1352

your partial should inherit from type ViewPage<Company> and then you can pass the current company direct as viewmodel. The ViewDataDictionary of the hosting page will passover to the partial automatically.

<% Html.RenderPartial("FundList", company); %>

Upvotes: 1

sharp johnny
sharp johnny

Reputation: 814

How about just moving the foreach into your partial view, removing the need for this viewdata? Looks like the cleanest solution to me.

If not, are you sure you actually need the "Model"? Isn't the company supposed to be your model while the other stuff should not be concerning the partial view?

Otherwise, go for a viewmodel containing all properties needed.

Upvotes: 1

Related Questions