ASP.NET MVC 2 - HTML.EditorFor()和自定义EditorTemplates [英] ASP.NET MVC 2 - HTML.EditorFor() and Custom EditorTemplates

查看:150
本文介绍了ASP.NET MVC 2 - HTML.EditorFor()和自定义EditorTemplates的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MVC 2的另外的HtmlHelper EditorFor(中),这是不可能创建一个给定的模型对象强类型的显示和编辑模板,并与它摆弄后,我有点难倒就如何传递额外的模型数据的编辑不失编辑器控件的强类型。

With MVC 2's addition of the HtmlHelper EditorFor() it is not possible to create strongly typed Display and Editor templates for a given Model object and after fiddling with it I am a bit stumped as to how to pass additional Model data to the editor without losing the strong-typing of the editor control.

经典案例:产品具有种类。 ProductEditor有包含所有类别的名称类别一个DropDownList。该ProductEditor是强类型的产品,我们需要在类别中选择列表,以及产品通过。

Classic Example: Product has Category. ProductEditor has a DropDownList for Category containing the names of all Categories. The ProductEditor is strongly typed to Product and we need to pass in the SelectList of Categories as well as the Product.

通过一个标准的看法,我们将包装在一个新的类型的模型数据,并通过沿。随着EditorTemplate我们失去了一些标准的功能,如果我们传递一个包含多个对象的混合模型(我注意到的第一件事是所有的LabelFor / TextBoxFor方法进行生产,如Model.Object实体名称,而不是仅仅对象)。

With a standard view we would wrap the Model data in a new type and pass that along. With the EditorTemplate we lose some of the standard functionality if we pass in a mixed Model containing more than one object (first thing I noticed was all of the LabelFor/TextBoxFor methods were producing entity names like "Model.Object" rather than just "Object").

我是不是做错了还是应该Html.EditorFor()有一个附加的ViewDataDictionary /型号参数?

Am I doing it wrong or should Html.EditorFor() have an additional ViewDataDictionary/Model parameter?

推荐答案

您可以创建一个自定义视图模型有两个属性,或者您需要使用的ViewData来传递这些信息。

You can either create a custom ViewModel which has both properties OR you'll need to use ViewData to pass that information in.

这篇关于ASP.NET MVC 2 - HTML.EditorFor()和自定义EditorTemplates的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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