c# - Default selection in RadioButtonFor -


i generating radiobutton list , trying select 1 option on load below.

foreach loop in view

@foreach (var mylistitem in model.mylist)  {     @html.radiobuttonfor(m => m.mytype,mylistitem.mytype, new {id = mylistitem.mytype, @checked = (model.mytypeid == mylistitem.mytypeid) })     @mylistitem.mytype  } 

eventhough html generated correctly(refer below). second option checked instead of 1st when model.mytypeid = 0.

generated html view

<input id="0" name="mytype" value="option one" checked="true" type="radio">option 1 <input id="1" name="mytype" value="option 2  " checked="false" type="radio">option 2     

please suggest how else can select desired radio button option deafult.

the html isn't correct actually. need more along these lines:

@foreach (var mylistitem in model.mylist) {     if (model.mytypeid == mylistitem.mytypeid)     {         @html.radiobuttonfor(m => m.mytype,mylistitem.mytype,             new             {                 id = mylistitem.mytype,                 @checked = ""             })     }     else     {         @html.radiobuttonfor(m => m.mytype,mylistitem.mytype,             new             {                 id = mylistitem.mytype,             })     }     @mylistitem.mytype } 

though can't verify exact output, should this:

<input id="0" name="mytype" value="option one" checked type="radio"> 

you may have use null generate checked without ="", okay too. see, it's not value that's recognized, it's attribute itself, that's why second one checked.


Comments