model view controller - MVC Set Selected Value of @Html.DropDownListFor to Querystring value -


i think i'm there, reason dropdown defaulting first item in list instead of value i'm passing (defaultchoice). ideas?

@{     var defaultchoice = request.querystring["id"];     var choices = new selectlist(viewbag.choices, "myitemid", "myproductname", defaultchoice); }   @html.dropdownlistfor(m =>m.myitemidprimary, choices) 

final code generates , error:

string defaultchoice = request.querystring["id"]; int idval = int.parse(defaultchoice); var choices = new selectlist(viewbag.choices, "myitemid", "myproductname", idval);  model.myitemidprimary = idval; 

i error: value cannot null (on int idval = int.parse(defualtchoice) line)

you can assign value model.

like this

@{     var defaultchoice = request.querystring["id"];     var choices = new selectlist(viewbag.choices, "myitemid", "myproductname", defaultchoice);    model.myitemidprimary=defaultchoice; } 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - How to Hide Date Menu from Datepicker in yii2 -