he añadido a su modelo un Selected
propiedad
he añadido un EditorTemplate
para mostrar una sola Field
lo que pasará ahora al momento de enviar, todos los artículos serán le envían pueden luego filtrar todo el artículo que tiene una propiedad de Selected=true
el Modelo
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; }
}
El controlador de[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();
}
The View[/Views/Test/Index.cshtml]
@model MvcApplication2.Models.TestModel
@using(@Html.BeginForm("XY","Test"))
{
@Html.EditorFor(m => m.Fields)
<input type="submit" value="submit"/>
}
El Editor de plantillas de[/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)