使用 MVC Contrib 的多视图模型/单视图
首先,这不是一个新问题。我的问题实际上是 gWiz 提供的答案之一的后续问题。他的帖子/答案的链接位于:
执行此操作的另一种方法是不对视图进行强类型化,并且 page 指令中的母版页,而是使用 来自 MVC Contrib 的基于通用类型的 ViewData 扩展。这些 扩展基本上使用完全限定的类型名称作为 ViewData 字典键。实际上,打字的好处与 强类型页面方法,在以下方面具有较少的类开销 所需的视图模型类的数量。然后在你的行动中你会这样做
ViewData.Add<汽车>(汽车); ViewData.Add
(layoutAData); 以及您所做的视图
<%= ViewData.Get
().Color %> 并在母版页中执行
<%= ViewData.Get
().用户名 %> 您可以缓存这些 Get<>在视图中内联调用以减轻 多次铸造的成本。
我的问题具体是关于最后一条评论:如何在视图中“缓存” get 调用?视图不是每次都会被销毁和创建吗?
我确实尝试寻找示例,但也许我没有问正确的问题?
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 comment: 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
他可能会说的是,而不是
你将它分配给一个变量并稍后使用该变量
What he might be saying is that instead of
You assign it to a variable and use the variable later