mercredi 5 septembre 2018

Get value from a dynamic checkbox list ASP.NET MVC

I made a view which relates the role of user with the user, I create the view with the dynamic checkboxes which you can register roles and then assign it to the user, only when sending the marked roles, in the controller I do not come back, mark me null, these are my classes.

public class CheckBoxClass
{
    public string Nombre_ChB { get; set; }
    public bool Value_Check { get; set; }
    public int Id_Check { get; set; }
    public string IdUsuario { get; set; }
}


public class CheckBoxList
{
  public List<CheckBoxClass> CheckBoxLista { get; set; }
}


    public ActionResult ListaCheckVistaParcial(string ID)
    {
        using (var db = new ProductoEntities())
        {
            var q= db.USUARIO_INTERFACEMTY.Where(x => x.USERID == ID).FirstOrDefault();

            return View(ListaCheckBox(q));
        }



[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult ListaCheckVistaParcial(CheckBoxList cb)
    {

        return RedirectToAction("Index", "Account");
    }

@model IEnumerable< InterfaceMonterrey.Models.CheckBoxList>

@{
    ViewBag.Title = "ListaCheckVistaParcial";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>ListaCheckVistaParcial</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Asignar Rol</h4>
        <hr />
 
        @{
            int i = 0;
        }
        @foreach (var item in Model)
        {
            if(i <= Model.Count())
            {
                <div class="form-group">
                @Html.Label(item.CheckBoxLista[i].Nombre_ChB, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    <div class="checkbox">
                        @Html.CheckBoxFor(model => item.CheckBoxLista[i].Value_Check)
                        @Html.ValidationMessageFor(model => item.CheckBoxLista[i].Value_Check, "", new { @class = "text-danger" })
                    </div>
                </div>
            </div>
                i++;
            } 
        }

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

enter image description here

enter image description here




Aucun commentaire:

Enregistrer un commentaire