dodałem do modelu z Selected
własność
mam dodał EditorTemplate
aby wyświetlić pojedynczy Field
, co będzie teraz, kiedy złożyć wszystkie elementy będą wysyłać można następnie odfiltruj wszystkie elementy, które mają właściwość Selected=true
Model
public class TestModel
{
public IList<Field> Fields { get; set; }
}
public class Field
{
public String Key { get; set; }
public String Value { get; set; }
public bool Selected { get; set; }
}
Kontroler[TestController.cs]
public ActionResult Index()
{
var testModel = new TestModel();
testModel.Fields = new List<Field>
{
new Field { Key = "Choice 1" , Selected = true , Value = "1"},
new Field { Key = "Choice 2" , Selected = false , Value = "2"},
new Field { Key = "Choice 3" , Selected = false , Value = "3"}
};
return View(testModel);
}
[HttpPost]
public ActionResult XY(TestModel model)
{
var selectedFields = model.Fields.Where(f => f.Selected);
/** Do some logic **/
return View();
}
Widok[/Views/Test/Index.cshtml]
@model MvcApplication2.Models.TestModel
@using(@Html.BeginForm("XY","Test"))
{
@Html.EditorFor(m => m.Fields)
<input type="submit" value="submit"/>
}
Szablon Edytor[/Views/Test/EditorTemplates/Field.cshtml]
@model MvcApplication2.Models.Field
<label>
@Html.CheckBoxFor(m =>m.Selected)
@Model.Key
</label>
@Html.HiddenFor(m =>m.Value)
@Html.HiddenFor(m =>m.Key)