开发者

Multiple ViewModels/Single View with MVC Contrib

First of all, this is not a new question. My question is actually a follow up question to one of the answers provided by gWiz. The link to his post/answer is here:

Multiple models sent to a single view instance

Another way to do this would be not to strongly-type the view and master pages in the page directive, but instead make use of the generic type-based ViewData extensions from MVC Contrib. These extensions basically use the fully-qualified type name as the ViewData dictionary key. Effectively, the typing benefits are the same as the strongly-typed page approach, with less class overhead in terms of the number of view model classes required. Then in your actions you do

ViewData.Add<Car>(car); ViewData.Add<LayoutAData>(layoutAData);

and in the views you do

<%= ViewData.Get<Car>().Color %>

and in the master page you do

<%= ViewData.Get<LayoutAData>().Username %>

You could cache these Get<> calls inline in the views to mitigate the cost of casting multiple times.

My question is specifically about the last comme开发者_如何学运维nt: How would one go about "caching" the get calls in the View? Doesn't the view get destroyed and created each time?

I did try searching for examples but maybe I wasn't asking the right question?


What he might be saying is that instead of

<%= ViewData.Get<Car>().Color %>
<%= ViewData.Get<Car>().Make %>

You assign it to a variable and use the variable later

<% var car = ViewData.Get<Car>(); %>

<%= car.Color %>
<%= car.Make %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜