c# - Bind Dictionary with list in viewmodel to checkboxes -




how bind dictionary , it's values per key checkboxes? can display them in httpget binding selected values again httppost doesn't seem work.

viewmodel

public class editviewmodel {     public foo foo { get; set; }     public dictionary<bar, list<barversioneditvm>> matrix { get; set; } }  public class barversioneditvm {     public int id { get; set; }     public string name { get; set; }     public string version { get; set; }     public bool issupported { get; set; } } 

view:

<form asp-action="edit"> <div class="row">  @foreach (var kvp in model.matrix.orderbydescending(x => x.key.name))  {    <div class="col-md-2 col-lg-2">      <fieldset>          <legend>@kvp.key.name</legend>         @foreach (var version in kvp.value)         {           <div>            <input type="checkbox" id="@version.id" value="@version.issupported" name="@version.name" @(version.issupported ? "checked=\"checked\"" : "") />                <label>@version.version:</label>          </div>         }     </fieldset>   </div>   }   </div> <input type="hidden" asp-for="@model.foo.id" /> <input type="submit" value="save" class="btn btn-default" /> </form> 

in view tried rewrite foreach , using html helpers, without success:

@html.checkboxfor(model => model.matrix[kvpair.key][i].issupported)

controller:

[httppost] [validateantiforgerytoken] public async task<iactionresult> edit(editviewmodel vm) {  // vm there matrix  null. // , id of foo property filled in. } 

any suggestions?

unless dictionary has simple value types both key , value (e.g. public dictionary<string, string>), defaultmodelbinder requires form control name attributes in format

<input .... name="matrix[0].key" value="..." /> <input .... name="matrix[0].value[0].id" value="..." /> <input .... name="matrix[0].value[0].name" value="..." /> 

there no htmlhelper methods generate correct html allow binding dictionary.

it far simpler create simple view model(s) ilist<t> properties collections. based on view have shown, models be

public class editvm {     public int fooid { get; set; }     public list<barvm> bars { get; set; } } public class barvm {     public string name { get; set; }     public list<barversionvm> versions { get; set; } } public class barversionvm {     public int id { get; set; }     public string name { get; set; } // not clear use property     public string version { get; set; }     public bool issupported { get; set; } } 

and view be

@model editvm .... @html.hiddenfor(m => m.fooid) @for(int = 0; < model.bars.count; i++) {     <fieldset>         <legend>@model.bars[i].name</legend>         @html.hiddenfor(m => m.bars[i].name) // in case need return view in post method         @for(int j = 0; j < model.bars[i].versions.count; j++)         {             <div>                 @html.hiddenfor(m => m.bars[i].versions[j].id)                 @html.checkboxfor(m => m.bars[i].versions[j].issupported)                 @html.labelfor((m => m.bars[i].versions[j].issupported, model.bars[i].versions[j].version)             </div>         }     </fieldset> } <input type="submit" value="save" /> 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -