我在我的项目中有一个枚举,我为这个枚举创建了一个自定义编辑器模板。所以,现在我有一个类型的这个枚举的属性,将渲染一个下拉列表。
这很好,但我想用属性的名称命名我的下拉菜单中的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>
所以,我选择元素id和name属性的“PropertyNameHere”,我想拥有我的模型的属性的名称。这里是一个例子:
我的型号:
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 )
我想要我的select元素的名称和id为’MyItemType’。
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。