Thursday, 26 December 2013

Binding Model to the View in ASP.NET MVC

              In this article we are going to see the Model binding to the View from the controller, In MVC architecture Model, View and Controller are the three important things plays are major role in design a web application. so in this article we will see some samples of strongly typed data to the view from the controller by creating the Employee model.

In View we are going to display a employee information, where data is passed from the controller to the view by the strongly typed data model.

In controller we are going to create a employee object and send that object to the view by as parameter to the view(emp); In the View we are strongly typed the employee object so the employee data is fetched from the Model class.


   public class Employee
        public int EmployeeId { set; get; }

        public string EmployeeName { set; get; }

        public string Country { set; get; }

        public string Married { set; get; }


    public class EmployeeController : Controller
        public ActionResult GetEmployee()
            Employee emp = new Employee() {
EmployeeId = 1,
            return View(emp);     



@model TestingMvc.Models.Employee

    ViewBag.Title = "Employee Data";

<h2>Employee Information</h2>

<table style="border:2px solid gray">
<td><b>ID</b></td><td style="padding:5px">@Model.EmployeeId</td>
<td><b>Name</b></td><td style="padding:5px">@Model.EmployeeName</td>
<td><b>Country</b></td><td style="padding:5px">@Model.Country</td>
<td><b>Married</b></td><td style="padding:5px">@Model.Married</td>



From this article we can learn to bind the model from the controller to the view.

No comments:

Post a Comment