c# - ASP.NET MVC - Set default value for static Select (dropdownlist) -


i using regular html drop downlist (select) in view. don't want use html.dropdownlistfor reason. able selected value in controller class. how assign selected value view?

<select name='ddllocation' id='ddllocation'>   <option value="">all </option>   <option selected="selected" value="1">city1</option>   <option value="2">city2</option>   <optgroup label="group">     <option value="3">city2</option>     <option value="4">city3</option>     <option value="5">city4</option>   </optgroup> </select> 

well there go, you've done using selected attribute:

<option selected="selected" value="1">city1</option> 

you should put selected attribute on value want preselected.

i guess realize madness of not using html.dropdownlistfor helper automatically handles you. because you're gonna have put @if else stataments turning view spaghetti code in order achieve desired behavior. guess you've got reasons not using helper.


update:

from comment seems reason not using dropdownlist helper because doesn't support optgroup. that's true , reason. writing custom helper trivial , avoid spaghettifying views if/else statements preselecting default value.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -