我是asp.net MVC的新手。我试图在我的视图页面上使用下拉控件,该页面从枚举中填充。我还想为下拉列表添加自定义描述。我搜索了很多例子,但没有人发布如何在视图页面上填充描述。这是我的代码:
视图模型:
public enum SearchBy
{
[Description("SID/PID")]
SID = 1,
[Description("Name")]
Name,
[Description("Birth Date")]
DOB,
[Description("Cause#")]
Cause
}
Index.cshtml
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group form-inline">
@Html.LabelFor(model => model.searchBy, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EnumDropDownListFor(model => model.searchBy, "Search By", htmlAttributes: new { @class = "form-control" })
@Html.TextBox("searchByVal", null, htmlAttributes: new { @placeholder = "SID / PID ", @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @placeholder = "First Name", @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @placeholder = "Last Name", @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DOB, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DOB, new { htmlAttributes = new { @placeholder = "Birth Date", @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CauseNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CauseNumber, new { htmlAttributes = new { @placeholder = "Cause#", @class = "form-control" } })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Search" class="btn btn-block btn-primary" />
</div>
</div>
</div>
它没有填充我的SearchBy枚举中提到的with description字段。看到这里的图像。 http://postimg.org/image/phdxgocj7/ 请帮助我,我犯了错误。谢谢
更新 我从Nico那里得到了解决方案。我对此进行了一些研究。我正在使用解决方案更新此帖子,因为它可能对其他人有用,他们是MVC的新手 http://weblogs.asp.net/jongalloway//looking-at-asp-net-mvc-5-1-and-web-api-2-1-part-1-overview-and-enums
谢谢大家。享受编码..
答案 0 :(得分:22)
Html帮助器EnumDropDownListFor
或EnumDropDownList
未考虑Description
成员上的enum
属性修饰。但是,通过查看源代码:
Enum下拉列表助手: https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Html/SelectExtensions.cs
Enum Helper Classes: https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Html/EnumHelper.cs
上面的枚举助手类用于将Enum
转换为List<SelectListItem>
。从下面的代码:
// Return non-empty name specified in a [Display] attribute for the given field, if any; field's name otherwise
private static string GetDisplayName(FieldInfo field)
{
DisplayAttribute display = field.GetCustomAttribute<DisplayAttribute>(inherit: false);
if (display != null)
{
string name = display.GetName();
if (!String.IsNullOrEmpty(name))
{
return name;
}
}
return field.Name;
}
您可以在方法GetDisplayName
中看到它检查DisplayAttribute
成员上是否存在enum
。如果display属性存在,则将名称设置为DisplayAttribute.GetName()
方法的结果。
将这些放在一起,我们可以修改enum
以使用DisplayAttribute
代替DescriptionAttribute
并将Name
属性设置为您希望显示的值。
public enum SearchBy
{
[Display(Name = "SID/PID")]
SID = 1,
[Display(Name = "Name")]
Name,
[Display(Name = "Birth Date")]
DOB,
[Display(Name = "Cause#")]
Cause
}
这可以为您提供所需的结果。
希望这有帮助。
答案 1 :(得分:2)
鉴于:
public enum MyEnum
{
[Description("This is the description if my member A")]
A,
[Description("This is the description if my member B")]
B
}
我个人使用这种扩展方法从我的枚举中获取描述:
public static string GetDescription(this Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
{
return attributes[0].Description;
}
else
{
return value.ToString();
}
}
使用它:
MyEnum.A.GetDescription();
希望得到这个帮助。
答案 2 :(得分:2)
我创建了一个尝试不同类型属性的辅助类。我需要它,因为我使用了https://github.com/civicsource/enums和https://silviomoreto.github.io/bootstrap-select/
的引导程序public static class EnumHelper<T>
{
static EnumHelper()
{
var enumType = typeof(T);
if (!enumType.IsEnum) { throw new ArgumentException("Type '" + enumType.Name + "' is not an enum"); }
}
public static string GetEnumDescription(T value)
{
var fi = typeof(T).GetField(value.ToString());
var attributes = (DescriptionAttribute[]) fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
return attributes.Length > 0 ? attributes[0].Description : value.ToString();
}
public static IEnumerable<SelectListItem> GetSelectList()
{
var groupDictionary = new Dictionary<string, SelectListGroup>();
var enumType = typeof(T);
var fields = from field in enumType.GetFields()
where field.IsLiteral
select field;
foreach (var field in fields)
{
var display = field.GetCustomAttribute<DisplayAttribute>(false);
var description = field.GetCustomAttribute<DescriptionAttribute>(false);
var group = field.GetCustomAttribute<CategoryAttribute>(false);
var text = display?.GetName() ?? display?.GetShortName() ?? display?.GetDescription() ?? display?.GetPrompt() ?? description?.Description ?? field.Name;
var value = field.Name;
var groupName = display?.GetGroupName() ?? group?.Category ?? string.Empty;
if (!groupDictionary.ContainsKey(groupName)) { groupDictionary.Add(groupName, new SelectListGroup { Name = groupName }); }
yield return new SelectListItem
{
Text = text,
Value = value,
Group = groupDictionary[groupName],
};
}
}
}
你称之为:
<div class="form-group">
@Html.LabelFor(model => model.Address.State, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-sm-4">
@Html.DropDownListFor(model => model.Address.State, EnumHelper<StateProvince>.GetSelectList(), new { @class = "selectpicker show-menu-arrow", data_live_search = "true" })
@Html.ValidationMessageFor(model => model.Address.State, "", new { @class = "text-danger" })
</div>
</div>
答案 3 :(得分:2)
如果您使用的是.Net Framework 4.0
或更高版本,则无需创建帮助器类。
您可以将Display
属性与EnumDropDownListFor
一起使用
public enum SearchBy
{
[Display(Name = "SID/PID")]
SID = 1,
[Display(Name = "Name")]
Name,
[Display(Name = "Birth Date")]
DOB,
[Display(Name = "Cause#")]
Cause
}
在您看来:
@Html.EnumDropDownListFor(model => model.SearchBy, "Search By", new { @class = "form-control" })
Microsoft文档: