What is Model

In MVC ‘M’ stands for Model. In MVC model is used to create logic class for any MVC application. We can put all our project logic called business logic in model in MVC.

In this example I am creating model in a simple way. In my next article I will discuss model with strongly view.

For this example I am taking controller name Home and method name is Index and create view for this index method.

Add Model

To add model Right click on model folder from solution explorer ->go to add->class

		
	model in MVC
		Figure 1
		
		
		

Give a proper name to the class.

Model Class Code

I have given the name EmployeeData. In this class I have created some variable to store data. The code of this class is as follows:-

		
	using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplication5.Models
{
    public class EmployeeData
    {
        public int EmpId;
        public string EmpName;
        public string EmpDept;
        public int salary;
    }
}
		
		

Code of Controller

Now I go to the index method and create the object of this class EmployeeData and assign some data to this object and pass this object to the view as I want to show this data on this view.

		
	using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication5.Controllers
{
    public class HomeController : Controller
    {
       
        public ActionResult Index()
        {
            //create object of this model. 
            MvcApplication5.Models.EmployeeData emp = new Models.EmployeeData();
            emp.EmpId = 101;
            emp.EmpName = "isha";
            emp.EmpDept = "Developer";
            emp.salary = 12000;

            //pass this object to the view
            return View(emp);
        }

    }
}

   
		
		

Code of Index View

Now go to the view and create the object of this class. To create the object of any model on view we use @model.

After that use this object @model to show data on view which is as follows:-

		
		@model MvcApplication5.Models.EmployeeData

@{
    Layout = null;
    
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        <table border="1">
            <tr>
                <th>Employee ID</th>
                <th>Employee Name</th>
                <th>Employee Dept</th>
                <th>Employee Salary</th>
           </tr>
            <tr>
                <td>@Model.EmpId  </td>
                <td>@Model.EmpName  </td>
                <td>@Model.EmpDept  </td>
                <td>@Model.salary  </td>
            </tr>
        </table>

    </div>
</body>
</html>

		
		
		

As you can see that I have created a table in which in first row I have printed the column name and in second row I have showed the records using @model object which I passed to this view from index method.

The output of this code as follows:-

		
		Model Example in MVC
		Figure 1

		
		

As you can see that it’s showing records of that object. Note:-we must have to pass the object of model to the view otherwise it will show the error.

Email Address

For any query you can mail me at info@techaltum.com