传递额外的数据EditorTemplate [英] Passing extra data to EditorTemplate

查看:112
本文介绍了传递额外的数据EditorTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我有一个LineItem的(从过度使用购物车的例子),我想用EditorTemplate来呈现它。

Lets say I have a LineItem (from the over used Shopping Cart example) and I want to render it using a EditorTemplate.

我是完全没有问题使用@ Html.EditorFor(M => m.LineItems)从父图(局部或其他方式)的渲染,但什么是混淆是最好的方法来传递一些额外的数据(说的SelectList具有数据从数据库)来在对模板

I am perfectly alright rendering it using a @Html.EditorFor(m => m.LineItems) from the parent view (partial or otherwise), but what is confusing is the best approach to pass some extra data (say a SelectList which has data coming in from the database) to the template.

显然,我不应该污染通过将这些多余物品的LineItem模型(这不过是从视图的角度必需的。)

Clearly I should not be polluting the LineItem model by adding these extraneous items (which are however required from a view's perspective.)

我想看看是否有诉诸ViewBag / ViewData的技巧之前这样做的强类型的方式。

I am trying to see if there is a strongly typed way of doing this before resorting to ViewBag/ViewData tricks.

我试图创造一个LineItem的特定视图模型来传递数据,但它轧液通过MVC和广告的额外层到集合(我传递一个IEnumerable的&LT生成的名称;>视图模型来在EditorFor()调用,而不是IEnumerable的<保证实际的LineItem的>)

I have tried creating a 'LineItem' specific view model to pass the data in, but it mangles the names being generated by MVC and ads an extra layer to the collection (as I pass in a IEnumerable<> of the viewmodel to the EditorFor() call, instead of IEnumerable<> of the actual LineItem)

此外,这是一个错误的使用EditorTemplate的?是一个需要有从数据库表来吃不消EditorTemplate选项的下拉列表中的LineItem?

Also, is this a wrong use of the EditorTemplate? Is a LineItem requiring a dropdown which has options coming from a database table too much for a EditorTemplate?

请指引我走向MVC佳境。虽然我等待的答案,我将试训其他的想法!

Please guide me towards the MVC nirvana. While I wait for the answers, I will tryout other ideas!

要澄清:我使用EditorTemplate考虑的原因是因为它使我有自动收集处理。否则,整个[ID]商家变得太粘。

To clarify: The reason I am considering using EditorTemplate is because of the automatic collection handling which it affords me. Otherwise the whole [id] business becomes too sticky.

推荐答案

我最近面临同样的问题,发现<一个href=\"http://weblogs.asp.net/jgalloway/archive/2010/07/01/using-viewmodel-information-in-an-asp-net-mvc-2-editor-or-display-template.aspx\">this解决方案。

I was facing the same problem recently and found this solution.

要总结一下,您应该创建自定义视图模型包装原始的采集和必要的支持数据。然后在呼唤您可以使用自定义模板的HtmlHelper 扩展方法重载传递支持数据的的ViewData 字典视图。从您的自定义模板然后你可以检索和使用它们frin 的ViewData

To summarize it, you should create your custom view model wrapping the original collection and the required supporting data. Then in the view calling the custom template you can use HtmlHelper extension method overloads to pass the supporting data to the ViewData dictionary. From your custom template you can then retrieve and use them frin ViewData.

我觉得这是一个很好的解决方法,基本上这是视图模型被用于,至少它比延长或从原来的类派生只是为视图提供额外的数据更加好。

I think this is a good workaround and basically this is what view models are intended for, at least it is much more better than extending or deriving from the original class just to provide extra data for the view.

请出租或我知道,如果它适合你的情况下,如果你发现任何其他解决方案。

Please let me know if it fits your scenario or if you find any other solution.

这篇关于传递额外的数据EditorTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆