so I've looked everywhere for an answer to this peculiar problem, but I couldn't find the exact solution. Lots of similar problems, but not one that solves my exact problem, so apologies if this has already been answered.
I have the following (simplified) model:
public class SimpleModel {
public List<ListItemModel> Items { get; set; }
}
public class ListItemModel {
public string Text { get; set; }
public string Value { get; set; }
public bool Selected { get; set; }
}
And the following (simplified) view:
@for(var i = 0; i < Model.Items.Count; i++) {
@Html.CheckBoxFor(
m => Model.Items[i].Selected,
new { data_val = Model.Items[i].Value })
@Html.HiddenFor(m => Model.Items[i].Text)
}
And this produces the expected markup:
<input name="Fields[5].Items[0].Selected" id="Fields_5__Items_0__Selected" type="checkbox" value="true" data_val="89">
<input name="Fields[5].Items[0].Selected" type="hidden" value="false">
<input name="Fields[5].Items[0].Text" id="Fields_5__Items_0__Text" type="hidden" value="Option 1">
And when this is posted to the controller, I receive the following:
- form-data: "true,false"
- model.Fields[5].Items[0].Selected: false
I have had to write a method to populate my collection with the data from the form, but I feel that Razor should be taking care of this for me, but it doesn't seem to be working.
What might I be missing?
Thank you
Aucun commentaire:
Enregistrer un commentaire