Below is the model class for Employee. Pretty Simple!
public class Employee
{
[Required]
public int EmpID { get; set; }
[DisplayName("Name")]
[Required]
public string EmpName { get; set; }
[DisplayName("Bussiness Segment")]
[Required]
public string EmpDept { get; set; }
[Required]
public string Salary { get; set; }
[DisplayName("Designation")]
[Required]
public string EmpLevel { get; set; }
}
And below is the view I have to display list of employees which I have created like in Index action method of Employee Controller – right click and add View and then select strongly type view and then select Employee model class.
@model IEnumerable<MvcAppModelBinding.Models.Employee>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<fieldset>
<legend>Employee</legend>
@foreach (var item in Model)
{
<div class="display-label">EmpID</div>
<div class="display-field">@item.EmpID</div>
<div class="display-label">EmpName</div>
<div class="display-field">@item.EmpName</div>
<div class="display-label">EmpDept</div>
<div class="display-field">@item.EmpDept</div>
<div class="display-label">Salary</div>
<div class="display-field">@item.Salary</div>
<div class="display-label">EmpLevel</div>
<div class="display-field">@item.EmpLevel</div>
}
</fieldset>
<p>
@Html.ActionLink("Edit", "Edit", new { /* id=Model.PrimaryKey */ }) |
@Html.ActionLink("Back to List", "Index")
</p>
</body>
</html>
Now in the Employee Controller Index is the action method where I am display the above view.
The below code is in Employee Controller class.
// GET: /Employee/
// Static list to display multiple Employee records
public static List<Employee> lstEmp = new List<Employee>();
public ActionResult Index([Bind(Exclude = "EmpLevel")]Employee emp)
{
lstEmp.Add(emp);
return View(lstEmp);
}
The only new thing is Bind which applied on the class level. Like Exclude you also have include and prefix tag here. Here all the properties except designation/EmpLevel will be displayed. So I am hiding ‘EmpLevel’ property of Employee class model from controller.