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?