3
I’m trying to send all my View Checkbox, however, I can send only those selected via Formcollection
Controller
public ActionResult Index()
{
List<ListaCheckBox> listacheck = new List<ListaCheckBox>();
Random rand = new Random();
for (int i = 0; i < 4; i++)
{
ListaCheckBox listaDemo = new ListaCheckBox
{
CD_CHECKBOX = i,
CD_SELECIONADO = rand.Next(0,1),
DS_CHECKBOX = "CHECK" + i
};
listacheck.Add(listaDemo);
}
return View(listacheck);
}
[HttpPost]
public ActionResult Index(FormCollection form,ListaCheckBox listaDemo)
{
string[] formCollec = form["chek"].Split(',');
return View();
}
Model
public class ListaCheckBox
{
public int CD_CHECKBOX { get; set; }
public string DS_CHECKBOX { get; set; }
public int CD_SELECIONADO { get; set; }
}
View
@model List<Testes.Models.ListaCheckBox>
@using (Html.BeginForm())
{
@foreach (var item in @Model)
{
<label for="chek">@item.DS_CHECKBOX.ToString()</label>
<input type="checkbox" name="chek" id="check" value="@item.CD_CHECKBOX" />
<br />
}
<br />
<input type="submit" value="Envia CheckBox" />
}
My FormColletion
is receiving, but only the items that were selected. I would like to see the ones that were not selected as well. What I am doing wrong?
The list is coming empty "- listDemo null System.Collections.Generic.List<Tests.Models.Listcheckbox> "
– Andre Alves
@Andrealves, try to change
listaDemo
forindex
. The parameter name must be equal toname
form.– Thiago Lunardi