This question already has an answer here:
Let me first present the essential parts of my application:
This is my view:
Here is the code of my view:
@using MvcApplication6.Models;
@model List<Employee>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
<a href="@Url.Action("Create")">Create Entry</a>
</p>
<p>@Html.ActionLink("Check departments", "Index", "Department")</p>
<p><a href="@Url.Action("Summary")">Check summary</a></p>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<table border="1">
<tr>
<th>
@Html.DisplayNameFor(model => model[0].id)
</th>
<th>
@Html.LabelFor(model => model[0].firstname)
</th>
<th>
@Html.LabelFor(model => model[0].lastname)
</th>
<th>
@Html.DisplayNameFor(model => model[0].gender)
</th>
<th>
@Html.DisplayNameFor(model => model[0].department)
</th>
<th>Action</th>
</tr>
@foreach (Employee mod in Model)
{
<tr>
<td>
<input type="hidden" value="@mod.id" name="id" id="id" />
@Html.DisplayFor(modelItem => mod.id)
</td>
<td>
@Html.DisplayFor(modelItem => mod.firstname)
</td>
<td>
@Html.DisplayFor(modelItem => mod.lastname)
</td>
<td>
@Html.DisplayFor(modelItem => mod.gender)
</td>
<td>
@Html.DisplayFor(modelItem => mod.department)
</td>
<td>
<button onclick="location.href='@Url.Action("Edit", new { id = mod.id })';return false;">Edit</button> |
@Html.CheckBoxFor(modelItem => mod.selected)
</td>
</tr>
}
</table>
<br />
<input type="submit" value="submit" />
}
Here is my model:
[Table("carl")]
public class Employee
{
public int id { get; set; }
[DisplayName("First Name")]
[Required(ErrorMessage = "First name is required")]
public string firstname { get; set; }
[DisplayName("Last name")]
[Required(ErrorMessage = "Last name is required")]
public string lastname { get; set; }
[DisplayName("Gender")]
[Required(ErrorMessage = "Gender is required")]
public string gender { get; set; }
[DisplayName("Department")]
[Range(1, 3)]
[Required(ErrorMessage = "Dep name is required")]
public int department { get; set; }
[NotMapped]
public bool selected { get; set; }
}
I am using Entity Framework
and my context class is like this:
public class EmployeeContext: DbContext
{
public DbSet<Employee> Employees { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<gender> Genders { get; set; }
}
And lastly, here are is controller:
[HttpGet]
public ActionResult Index()
{
EmployeeContext emp = new EmployeeContext();
var adep = emp.Employees.ToList();
return View(adep);
}
[HttpPost]
public String Index(IEnumerable<Employee> emp)
{
if (emp.Count(x => x.selected) == 0)
{
return "0 selected";
}
else
{
return "Success";
}
}
Errors that i get is Additional information: Value cannot be null.
.
What i want to happen is that, all the selected checkboxes
to be deleted once the user submits. I made two separate types of index for checking. There is not much functionality yet because i want to confirm it first if i am able to bind to the model. Is it because of the [NotMapped]
Attribute? My database has id,firstname,gender,department
and i don't want to add another column just for the sake of having some "flags".
The html helper for my checkbox is this line of code:
@Html.CheckBoxFor(modelItem => mod.selected)
And i attached them on every single table rows.
Aucun commentaire:
Enregistrer un commentaire