我在我的项目中有一个枚举,并且为此枚举创建了一个自定义编辑器模板。所以,现在我有一个属性与这种枚举类型的任何模型,将呈现一个下拉。如何在我的MVC3自定义编辑器模板中获取属性名称
这很好,但我想用属性的名称命名我的下拉列表中的select元素。这是我的编辑器模板的Razor代码。
@model ItemEnumerations.ItemType
<select id="PropertyNameHere" name="PropertyNameHere">
@foreach (ItemEnumerations.ItemType in Enum.GetValues(typeof(ItemEnumerations.ItemType))) {
<option value="@value" @(Model == @value ? "selected=\"selected\"" : "")>@value.ToString()</option>
}
</select>
所以,在这里我有“PropertyNameHere”为选择元素id和name属性,我想有我的模型的属性的名称。这里有一个例子:
我的模型:
public class MyModel{
public int ItemID {get;set;}
public string ItemName {get;set;}
public ItemEnumerations.ItemType MyItemType {get;set;}
}
我查看代码:
@model MyModel
@Html.LabelFor(m => model.ItemID)
@Html.DisplayForm(m => model.ItemID)
@Html.LabelFor(m => model.ItemName)
@Html.EditorFor(m => model.ItemName)
@Html.LabelFor(m => model.MyItemType)
@Html.EditorFor(m => model.MyItemType)
我想我的选择元素有 'MyItemType' 的名称和ID。
在编辑器模板获得属性的名称也许更好的解决办法是延长DropDownFor方法来处理我的枚举? –
试试这个...http://blog.nathan-taylor.net/2012/10/aspnet-mvc-get-field-name-and-id-for.html –