How to Send Controller Checkbox?

Asked

Viewed 1,616 times

1

I have a View where in a client list there is in the first column (table) a Checkbox so that it may be possible to select one or more customers and send to Controller.

In View I am using the Checkbox pure and in the model this same field is owns the property public bool Selecao {get; set;} .

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

Note: Each one Checkbox is already receiving the customer ID in your property value.

  • The question that remains is chekbox´s, are several ? Your model is like, put your full code ?

2 answers

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 selected Id:

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

This way you will receive a list of Id of all those selected. Redo the search in your database and change the required data.

  • Exemplo should not have the Id customer? Shouldn’t it be a List<Exemplo>?

  • @I made a big mess, I already solved it by making another answer

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 " }
}

Controler:

public ActionResult MeuMetodo(ClienteViewModel model)
{
    var selecionados = model.Clientes.Where(m => m.Selecionado).Select(m => m.ClienteId);
    ...
}
  • Thanks Eduardo for the tip!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.