so I've been trying to use a checkbox to send a list of values to a string property in my model. but for some reason, the values are sent, even after i initialized with the bool false and left the checkbox unchecked.
these are my models
public class Profesor
{
public List<AsignaturaModelo> Asignaturas { get; set; }
public Profesor()
{
Asignaturas = new List<AsignaturaModelo>();
}
}
public class AsignaturaModelo
{
public string NombreA { get; set; }
public bool Selected { get; set; }
}
My controller
public ActionResult Create()
{
var model = new Profesor
{
Asignaturas = new[]
{
new AsignaturaModelo{NombreA = "Programacion 1", Selected = false},
new AsignaturaModelo {NombreA = "Programacion 2", Selected = false}
}.ToList()
};
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Profesor modelo)
{
try
{
if (ModelState.IsValid)
{
profesors.Add(modelo);
return RedirectToAction(nameof(Index));
}
}
catch
{
return View(modelo);
}
return View(modelo);
}
My view
@Html.EditorFor(x => x.Asignaturas)
and my EditorTemplate
@model AsignaturaModelo
<div>
@Html.CheckBoxFor(x => x.Selected)
@Html.HiddenFor(x => x.NombreA)
@Html.LabelFor(x => x.Selected, Model.NombreA)
</div>
Not sure what am i doing wrong, also sorry for the variable names. Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire