Jestem nowy w asp.net MVC. Próbuję użyć kontroli rozwijanej na mojej stronie widoku, która zapełnia się z enum. Chcę również dodać niestandardowe opisy do wartości rozwijanych. Przeszukałem wiele przykładów, ale nikt nie pisał, jak wypełnić opis na stronie widoku. Tu jest mój kodu:jak używać enum z DescriptionAttribute w 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>
Nie wypełniania pola opisu, jak wspomniano w moim SearchBy wyliczenia. zobacz zdjęcie tutaj. http://postimg.org/image/phdxgocj7/ Proszę mi pomóc, gdzie popełniam błąd. Dziękujemy
AKTUALIZACJA: Mam rozwiązanie tego od Nico. I trochę się o tym dowiedziałem. Aktualizuję ten wpis, ponieważ może być przydatny dla innych osób, które są nowe w firmie MVC http://weblogs.asp.net/jongalloway//looking-at-asp-net-mvc-5-1-and-web-api-2-1-part-1-overview-and-enums
Dziękuję wszystkim. Ciesz kodowania ..
Nie ma 'EnumDropDownListFor()' metoda w MVC -4. Masz na myśli MVC-5, czy jest to niestandardowy pomocnik? –
Przepraszamy, zaktualizowałem tag. Tak, używam MVC-5 –