I tried searching and didn't find anything that fixed my problem. I have a DropDownList on a Razor view that will not show the the item that I have marked as Selected in the SelectList. Here is the controller code that populates the list:
var statuses = new SelectList(db.OrderStatuses, "ID", "Name", order.Status.ID.ToString()); ViewBag.Statuses = statuses; return View(vm);
Here is the View code:
<div class="display-label"> Order Status</div> <div class="editor-field"> @Html.DropDownListFor(model => model.StatusID, (SelectList)ViewBag.Statuses) @Html.ValidationMessageFor(model => model.StatusID) </div>
I walk through it and even in the view it has the correct SelectedValue however the DDL always shows the first item in the list regardless of the selected value. Can anyone point out what I am doing wrong to get the DDL to default to the SelectValue?
Binding MVC DropDownList with Static Values Just add an Html helper for DropDownList and provide a static list of SelectListItem. The values added as SelectListItem will be added and displayed in the DropDownList. In this way, you do not need to add anything to Controller Action.
SelectListItem is a class which represents the selected item in an instance of the System. Web. Mvc.
The last argument of the SelectList
constructor (in which you hope to be able to pass the selected value id) is ignored because the DropDownListFor helper uses the lambda expression you passed as first argument and uses the value of the specific property.
So here's the ugly way to do that:
Model:
public class MyModel { public int StatusID { get; set; } }
Controller:
public class HomeController : Controller { public ActionResult Index() { // TODO: obviously this comes from your DB, // but I hate showing code on SO that people are // not able to compile and play with because it has // gazzilion of external dependencies var statuses = new SelectList( new[] { new { ID = 1, Name = "status 1" }, new { ID = 2, Name = "status 2" }, new { ID = 3, Name = "status 3" }, new { ID = 4, Name = "status 4" }, }, "ID", "Name" ); ViewBag.Statuses = statuses; var model = new MyModel(); model.StatusID = 3; // preselect the element with ID=3 in the list return View(model); } }
View:
@model MyModel ... @Html.DropDownListFor(model => model.StatusID, (SelectList)ViewBag.Statuses)
and here's the correct way, using real view model:
Model
public class MyModel { public int StatusID { get; set; } public IEnumerable<SelectListItem> Statuses { get; set; } }
Controller:
public class HomeController : Controller { public ActionResult Index() { // TODO: obviously this comes from your DB, // but I hate showing code on SO that people are // not able to compile and play with because it has // gazzilion of external dependencies var statuses = new SelectList( new[] { new { ID = 1, Name = "status 1" }, new { ID = 2, Name = "status 2" }, new { ID = 3, Name = "status 3" }, new { ID = 4, Name = "status 4" }, }, "ID", "Name" ); var model = new MyModel(); model.Statuses = statuses; model.StatusID = 3; // preselect the element with ID=3 in the list return View(model); } }
View:
@model MyModel ... @Html.DropDownListFor(model => model.StatusID, Model.Statuses)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With