gpt4 book ai didi

c# - 如何在 asp.net mvc 中使用带有 DescriptionAttribute 的枚举

转载 作者:可可西里 更新时间:2023-11-01 07:51:14 25 4
gpt4 key购买 nike

我是 asp.net MVC 的新手。我正在尝试在我的 View 页面上使用下拉控件,它从枚举中填充。我还想为下拉值添加自定义描述。我搜索了很多示例,但没有人发布如何在查看页面上填充描述。这是我的代码:

View 模型:

public enum SearchBy
{
[Description("SID/PID")]
SID = 1,
[Description("Name")]
Name,
[Description("Birth Date")]
DOB,
[Description("Cause#")]
Cause
}

索引.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 枚举中提到的那样填充描述字段。看这里的图片。 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

谢谢大家。享受编码..

最佳答案

Html 助手 EnumDropDownListForEnumDropDownList不考虑 Description enum 上的属性装饰成员。但是通过查看源代码:

枚举下拉列表助手: https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Html/SelectExtensions.cs

枚举助手类: https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Html/EnumHelper.cs

上面的枚举辅助类用于转换 EnumList<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 上成员。如果显示属性存在,则名称设置为 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
}

这会给你想要的结果。

enter image description here

希望这对您有所帮助。

关于c# - 如何在 asp.net mvc 中使用带有 DescriptionAttribute 的枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29615609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com