Sreedhar
Sreedhar

Reputation: 30025

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"]%>

Upvotes: 7

Views: 18112

Answers (1)

Samuel Neff
Samuel Neff

Reputation: 74909

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

Upvotes: 8

Related Questions