Search code examples
c#asp.net-mvcdrop-down-menuselectedvalue

ASP.NET MVC Drop Down List


I am making some changes to already existing ASP.NET MVC 1 app where there is a form with a dropdown list that has all 50 states hard coded into the HTML. After filling out the form the user can later go back and edit their information. I want to make it so that on the edit screen the value that is already in the DB gets the "selected" attribute for the state. The only way I can think to do this is to build the html on the server and send it down to the view, is there a better practice?


Solution

  • No, View is the only place where you should generate the markup.

    My suggestion is to write a JavaScript snippet like this:

     $(function(){ 
         $('#dropDownId').val(@Model.SelectedValue);
     });
    

    It's effective, easy, maintainable, and of course, fast.