Tôi mới dùng asp.net MVC. Tôi đang cố gắng sử dụng kiểm soát thả xuống trên trang xem của tôi, mà populates từ enum. Tôi cũng muốn thêm mô tả tùy chỉnh vào các giá trị thả xuống. Tôi đã tìm kiếm rất nhiều ví dụ, nhưng không ai đăng tải cách mô tả trên trang xem. Đây là mã của tôi:cách sử dụng enum với DescriptionAttribute trong 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>
Nó không phải là populating với các lĩnh vực mô tả như đã đề cập trong enum SearchBy tôi. xem hình ảnh ở đây. http://postimg.org/image/phdxgocj7/ Hãy giúp tôi, nơi tôi đang phạm sai lầm. Cảm ơn bạn
CẬP NHẬT: Tôi đã nhận được giải pháp cho việc này từ Nico. Và tôi đã nghiên cứu một chút về điều này. Tôi đang cập nhật bài đăng này bằng giải pháp vì nó có thể hữu ích cho những người khác, những người mới làm quen với MVC http://weblogs.asp.net/jongalloway//looking-at-asp-net-mvc-5-1-and-web-api-2-1-part-1-overview-and-enums
Cảm ơn tất cả. Tận hưởng mã hóa ..
Không có EnumDropDownListFor() 'phương pháp 'trong MVC -4. Bạn có nghĩa là MVC-5 hoặc là một trợ giúp tùy chỉnh? –
Rất tiếc, tôi đã cập nhật thẻ. Có, tôi đang sử dụng MVC-5 –