c# - Radiobutton controls not sent to viewmodel -


i have view users choose value radiobuttons. problem once user clicks submit, values in view model (in controller) null. view model follows:

public class preconditionsviewmodel {     public list<keyvaluepair<int, string>> provinces { get; set; }      [datatype(datatype.date)]     [displayformat(dataformatstring = "{0:dd/mm/yy}", applyformatineditmode = true)]     public datetime seedingdate { get; set; }      [datatype(datatype.date)]     [displayformat(dataformatstring = "{0:dd/mm/yy}", applyformatineditmode = true)]     public datetime harvestdate { get; set; }      public int drymatterpercentage { get; set; }      public int postalcode { get; set; }  } 

the action method: (for simplicity, have left method returning index. should able see model holds)

[httppost]     public actionresult save(preconditionsviewmodel model)     {                     return view("index");     } 

the view:

    @using (html.beginform("save", "precondition", formmethod.post, new { @class="userpreconditionform" })) {     foreach (var province in model.provinces)     {         @html.label(province.value, new { @class = "provinces" })         @html.radiobuttonfor(m => province.key, province.value)         }      <br/><br />      @html.labelfor(m => m.postalcode, resource.postalcode)     @html.textboxfor(m => m.postalcode)     <br/><br />      @html.label(resource.desiredseedingdate)     @html.textboxfor(m => m.seedingdate, new { @class="datefield", @readonly="true" } )     <br/><br />      @html.label(resource.latestharvestdate)     @html.textboxfor(m => m.harvestdate, new { @class = "datefield", @readonly = "true" })     <br/><br />      @html.label(resource.desireddrymatterpercentage)     @html.textboxfor(m => m.drymatterpercentage)      <br/><br/>      <input type="submit" value="@resource.continue" /> }  <br/> 

as can see, erros in code:

foreach (var province in model.provinces) {     @html.label(province.value, new { @class = "provinces" })     @html.radiobuttonfor(m => province.key, province.value)     } 

what trying here, create label , radiobutton each keyvaluepair in viewmodel. doing wrong, since not recieved in controller. doing wrong?

(i new asp.net mvc, critics , improvements welcome)


added edited code after @leftyx's reply

foreach (var province in model.provinces) {     @html.label("provinces_" + province.key, province.value, new { @class = "provinces" })     @html.radiobutton("province", province.key, new { id = "provinces_" + province.key })     } 

try add property province viewmodel preconditionsviewmodel:

public int province { get; set; } 

and in view change code radio buttons creation:

foreach (var province in model.provinces) {     @html.label("provinces_" + province.key, province.value, new { @class = "provinces" })     @html.radiobutton("province", province.key, new { id="provinces_" + province.key}) } 

each radio control rendered way:

<input id="provinces_1" name="province" type="radio" value="1"> <input id="provinces_2" name="province" type="radio" value="2"> 

as can see each 1 of inputs has different id same name.

when post form, added property province bound selected field.

you can try solution in zip file here.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -