How to Send a Checkbox to the Controller?

1

I have a View where a CheckBox exists in a client list in the first column so that it can be possible to select one or more clients and send it to > Controller .

In View I'm using pure CheckBox and in the model this same field has the public bool Selecao {get; set;} property.

My question is what is the correct way to send the selected chekbox's to the Controller , so I can perform the desired operations?

  

Note: Each CheckBox is already receiving the customer ID on your   property value.

    
asked by anonymous 13.06.2016 / 17:32

2 answers

2

I usually do something like this:

public class ClienteViewModel
{
    public ICollection<ClienteASelecionar> Clientes { get;set; }
}

public class ClienteASelecionar
{
    public bool Selecionado { get;set; }
    public int ClienteId { get; set; }
    public string NomeDoCliente { get; set; }
}

View:

for(i=0; i < Clientes.Count; i++)
{
    @Html.Label(Model.NomeDoCliente)
    @Html.CheckBox("Clientes["+ i + "].Selecionado")
    @Html.HiddenFor(m => m.ClienteId, new { @Name = "Clientes[" + i + "].ClienteId " }
}

Controller:

public ActionResult MeuMetodo(ClienteViewModel model)
{
    var selecionados = model.Clientes.Where(m => m.Selecionado).Select(m => m.ClienteId);
    ...
}
    
13.06.2016 / 20:12
2

Example:

Model:

public class Exemplo
{
    public int Id { get; set; }
    public string Nome { get; set; }
}

Controller and View:

public ActionResult Index()
{
    IList<Exemplo> exemplos = new List<Exemplo>();
    exemplos.Add(new Exemplo {Id = 1, Nome = "Nome 1"});
    exemplos.Add(new Exemplo { Id = 2, Nome = "Nome 2" });
    return View(exemplos);
}
@{ ViewBag.Title = "Index"; }
@using (Html.BeginForm("Resgatar", "Exemplo", FormMethod.Post))
{
    <table class="table">
        <tr>
            <td>
                @Html.Display("Selecionar")
            </td>
            <th>
                @Html.DisplayNameFor(model => model.Nome)
            </th>
            <th></th>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    <input type="checkbox" name="Ids" value="@item.Id" />
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Nome)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                    @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.Id })
                </td>
            </tr>
        }
        <tr>
            <td colspan="4">
                <button type="submit" class="btn btn-primary">Alterar</button>
            </td>
        </tr>
    </table>
}

Controller with ActionResult of Ids selected:

[HttpPost]
public ActionResult Resgatar(IEnumerable<int> Ids)
{
    return View();
}

In this way you will receive an Id list of all those selected. Please rebase the search on your database and change the required data.

    
13.06.2016 / 18:17