Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.DropDownList setting selected Item

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"]%>
like image 335
Sreedhar Avatar asked Dec 20 '10 00:12

Sreedhar


People also ask

How do I set SelectListItem selected?

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.


1 Answers

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

like image 181
Samuel Neff Avatar answered Sep 29 '22 02:09

Samuel Neff