<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Title", new SelectList(Enum.GetValues(typeof(OnLineShoppingCart.Models.Supplier.Salutation))), "Select The Salutation", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
Asked
Active
Viewed 96 times
0

Ehsan Sajjad
- 61,834
- 16
- 105
- 160

Salsero
- 21
- 5
-
2Can you please define _does not work_ exactly? You get an exception or error message or something? – Soner Gönül Mar 17 '15 at 09:22
-
this may help:http://stackoverflow.com/questions/28807472/get-enum-value-to-show-on-dropdownlist-asp-net-mvc – Ehsan Sajjad Mar 17 '15 at 09:24
-
well when I am in the enter screen and I press enter without filling any details in the fields e.g name, surname these fills give me a error message but nor the DropDownList – Salsero Mar 17 '15 at 23:45
1 Answers
2
try this:
Controller
IEnumerable<SelectListItem> TitleList = null;
TitleList = (from m in Enum.GetValues(typeof(Titles)).Cast<Titles>() select m).AsEnumerable().Select(m => new SelectListItem() { Text = m.ToString(), Value = m.ToString() });
ViewBag.TitleList = new SelectList(TitleList, "Value", "Text");
View
<div class="form-group">
@Html.LabelFor(m => m.Title, new { @class = "col-md-2 control-label" })
<div class="col-md-6">
@Html.DropDownListFor(m => m.Title, new SelectList(ViewBag.TitleList, "Value", "Text"), "--Select Title--", new { @class = "form-control", placeholder = "Title"})<br />
@Html.ValidationMessageFor(m => m.Title, null, new { @class = "text-danger" })
</div>
</div>

newton
- 21
- 3