How can I set selected Item of Html.DropDownList to Request.QueryString["JobType"]
<td class="data"><%= Html.DropDownList("MaintJobTypes")%>
some thing to:
<td class="data"><%= Html.DropDownList("MaintJobTypes").selectedItem = Request.QueryString["JobType"]%>
The trick is to set the Selected property when creating the SelectListItem Collection. It would not work any other way, perhaps I missed something but in end, it is more elegant in my option. Show activity on this post. Use LINQ and add the condition on the "selected" as a question mark condition.
If you set ViewData["MaintJobTypes"]
equal to the value of the item you want to select, Html.DropDownList
will pick that up and set the selected value. You can also use a property on Model
as long as the name of the drop down list matches the model property.
Alternatively you can use the SelectList
helper which provides a list of items and the selected one.
More examples and details available here:
http://codeclimber.net.nz/archive/2009/08/10/how-to-create-a-dropdownlist-with-asp.net-mvc.aspx
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