我对asp.net MVC还是个新手。我在尝试在我的视图页面上使用下拉控件,它从枚举中填充。我还想在下拉列表值中添加自定义描述。我搜索了很多示例,但没有人发布如何在视图页面上填充描述的帖子。以下是我的代码:
ViewModel:
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枚举中提到的那样填充说明字段。在此处查看图像。
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 谢谢大家。享受编码..
EnumDropDownListFor()
方法。你是指MVC-5还是这是一个自定义帮助程序? - user3559349