How can I set the selected value of a Html.DropDownListFor? I've been having a look online and have seen that it can be achieved by using the fourth parameter so like the below:
@Html.DropDownListFor(m => m, new SelectList(Model, "Code", "Name", 0), "Please select a country")
My select list then display like this:
<select id="ShipFromCountries" name="ShipFromCountries"> <option value="">Please select a country</option> <option value="GB">United Kingdom</option> <option value="US">United States</option> ... </select>
But for some reason United Kingdom remains selected but I want "Please select a country" to be selected.
Anyone know how I can achieve this?
I've updated my code as there was a slight change in functionality however still seem to be encountering this problem. This is what is in my view:
@Html.DropDownListFor(n => n.OrderTemplates, new SelectList(Model.OrderTemplates, "OrderTemplateId", "OrderTemplateName", 1), "Please select an order template")
1
is the Id of the option
that I want selected, I have also tried with the text of the option
but that also does not work.
Any ideas?
DropDownList(String, String, IEnumerable<SelectListItem>, Object, Object) Returns an HTML drop-down list control that has the specified name, custom attributes defined by an attribute object, and default selection, and that contains the specified list items and default item.
Your code has some conceptual issues:
First,
@Html.DropDownListFor(n => n.OrderTemplates, new SelectList(Model.OrderTemplates, "OrderTemplateId", "OrderTemplateName", 1), "Please select an order template")
When using DropDownListFor, the first parameter is the property where your selected value is stored once you submit the form. So, in your case, you should have a SelectedOrderId
as part of your model or something like that, in order to use it in this way:
@Html.DropDownListFor(n => n.SelectedOrderId, new SelectList(Model.OrderTemplates, "OrderTemplateId", "OrderTemplateName", 1), "Please select an order template")
Second,
Aside from using ViewBag, that is not wrong but there are better ways (put that information in the ViewModel instead), there is a "little bug" (or an unspected behavior) when your ViewBag property, where you are holding the SelectList, is the same name of the property where you put the selected value. To avoid this, just use another name when naming the property holding the list of items.
Some code I would use if I were you to avoid this issues and write better MVC code:
Viewmodel:
public class MyViewModel{ public int SelectedOrderId {get; set;} public SelectList OrderTemplates {get; set;} // Other properties you need in your view }
Controller:
public ActionResult MyAction(){ var model = new MyViewModel(); model.OrderTemplates = new SelectList(db.OrderTemplates, "OrderTemplateId", "OrderTemplateName", 1); //Other initialization code return View(model); }
In your View:
@Html.DropDownListFor(n => n.SelectedOrderId, Model.OrderTemplates, "Please select an order template")
For me was not working so worked this way:
Controller:
int selectedId = 1; ViewBag.ItemsSelect = new SelectList(db.Items, "ItemId", "ItemName",selectedId);
View:
@Html.DropDownListFor(m => m.ItemId,(SelectList)ViewBag.ItemsSelect)
JQuery:
$("document").ready(function () { $('#ItemId').val('@Model.ItemId'); });
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