Get checkbox record value in controller MVC C #

2

I have an MVC C # view in which I show the employee records of a specific region, plus I have added a checkbox so that the user can select one or more employees with whom I will work later.

this is the model I use in the MVC view

public class Developer
    {
        public int code { get; set; }
        public string areaDev { get; set; }
        public string nameDev { get; set; }
        public string expDev { get; set; }
        public string langDev { get; set; }
        public bool isChecked { get; set; }
    }

This is the controller from where I retrieve the information of the employees that I show in the view

 public ActionResult DeveloperView() {
            string conexion = ConfigurationManager.ConnectionStrings["cnn"].ConnectionString.ToString();
            List<WebApplication1.Models.Developer> Records = new List<Models.Developer>();
            SqlConnection sql = new SqlConnection();
            sql.ConnectionString = conexion;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = sql;
            cmd.CommandText = "select * from Developer";
            cmd.Connection.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    Models.Developer data = new Models.Developer();
                    data.code = Convert.ToInt32(dr["code"]);
                    data.areaDev = dr["areaDev"].ToString();
                    data.nameDev = dr["nameDev"].ToString();
                    data.expDev = dr["expDev"].ToString();
                    data.langDev = dr["langDev"].ToString();
                    data.isChecked = false;
                    Records.Add(data);
                }
            }
            return View(Records);
        }

and this is the view in which I show the information

@model IEnumerable<WebApplication1.Models.Developer>

@{
    ViewBag.Title = "DeveloperView";
}

<h2>DeveloperView</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
@using(Html.BeginForm("ShowRecords","Home"))
{

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.code)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.areaDev)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.nameDev)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.expDev)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.langDev)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.isChecked)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.code)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.areaDev)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.nameDev)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.expDev)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.langDev)
        </td>
        <td>
            @Html.CheckBoxFor(modelItem => item.isChecked)
        </td>
    </tr>
}
</table>
<input type="submit" value="Create" />
}

but when I press the submit button I do not receive any information in the ShowRecords controller, I receive NULL

this is the controller

 public ActionResult ShowRecords(List<WebApplication1.Models.Developer> datos)
        {
            List<WebApplication1.Models.Developer> lista = new List<Models.Developer>();
            foreach(var item in datos)  
            {  
                if(item.isChecked)  
                {
                    WebApplication1.Models.Developer data = new WebApplication1.Models.Developer();
                    data.code = item.code;
                    data.nameDev = item.nameDev;
                    data.areaDev = item.areaDev;
                    data.expDev = item.expDev;                   
                    data.langDev = item.langDev;
                    lista.Add(data);
                }            
            }  

            return View(lista); 
        }

How would you get the values of the records whose checkbox has been selected?

    
asked by Pablo Mayora 01.05.2017 в 00:33
source

2 answers

0

You have to change your view to:

@model IEnumerable<WebApplication1.Models.Developer>

@{
    ViewBag.Title = "DeveloperView";
}

<h2>DeveloperView</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
@using(Html.BeginForm("ShowRecords","Home"))
{

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.code)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.areaDev)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.nameDev)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.expDev)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.langDev)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.isChecked)
        </th>
        <th></th>
    </tr>

@for(var i = 0; i< Model.Count; i++) {
    <tr>
        <td>
            @Html.DisplayFor(m=> Model[i].code)
        </td>
        <td>
            @Html.DisplayFor(m=> Model[i].areaDev)
        </td>
        <td>
            @Html.DisplayFor(m=> Model[i].nameDev)
        </td>
        <td>
            @Html.DisplayFor(m=> Model[i].expDev)
        </td>
        <td>
            @Html.DisplayFor(m => Model[i].langDev)
        </td>
        <td>
            @Html.CheckBoxFor(m=> Model[i].isChecked)
        </td>
    </tr>
}
</table>
<input type="submit" value="Create" />
}
    
answered by 01.05.2017 в 02:38
-1

You try to send information by means of the submit button but you do not have a form, which should point to an action, in this case your ShowRecords method.

One solution I could give you is to use Jquery to go through your table and verify which rows have the check selected. You are generating an array of objects with the information of the row.

Once this is done, you would make a $.get() or $.ajax() , passing the respective data, in order to send the request to the method you want.

You can do something like for example:

$.ajax({
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        type: 'GET',
        url: '/Controller/ShowRecords',
        data: [], //Arreglo de los objetos seleccionados
        success: function (response) {          
            // Lógica que desees que haga
        },
        failure: function (response) {          
            // Error
        }
    }); 

Or you could also send all the rows and as you do in your action verify that objects have the field isChecked in true .

    
answered by 01.05.2017 в 02:38