Hi everyone in this article I’m explaining about Model class in MVC 4.


In this section I’ll teach you about model class. In my mind some question erase about this topic. What is model class, Why use model class, How to use model class.  If you want to learn more about View read my previous post Working with View.

What is model class?

Model class is a simple class that is manage database. These classes will be the model part of the ASP.NET MVC app. In this class you define some property for manage table columns. In mvc application for manage database three approach is available.

1.     Database First Approach

2.     Code First Approach

3.     Model First Approach

Why use model class?

Model class is use for manage database if you want to manage some student record in database you should use model class.

How to use model class?

You add model class inside Model folder. If you use model class you can install entity framework. Entity framework support development called code first. Code-first allows to you crate model object by writeing simple class if you learn more about code first approach you will read my previous post Crud Operation Using Code First. If you required to create database first you will read this post Crud Operation Using Database First.

Adding Model classes

In solution explorer right click on model folder, select Add, and then select class.


Give the class name and click ok.

Add the fallowing property in class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace StudentManagement.Models
    public class StudentDetails
        public int ID { get; set; }
        public string Name { get; set; }
        public string EmailId { get; set; }
        public string Address { get; set; }
        public string ContactNo { get; set; }


StudentDetails class is represent StudentDetail database. Now add StudentContext class. StudentClass represent the entity framework Student Database Context.

You can do this by manually adding the using statement, or you can right click on the red squiggly lines and clickResolve, and then click using System.Data.Entity.


Now you can use connection string in web.config file.

    <add name="DBConnection" connectionString="data source=MSCLIENT-006;initial catalog=MyDB;user id=sa;password=mindstick;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />


Now Add below code in HomeController.
using StudentManagement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace StudentManagement.Controllers
    public class HomeController : Controller
        // GET: /Home/
        StudentContext context;
        public HomeController()
            context = new StudentContext();
        public ActionResult Index()
            var student = context.Student.ToList();
            return View(student);


Now add index view
    ViewBag.Title = "Index";
<link href="~/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
    <h2 class="text-center text-primary">Teacher Details</h2>
    <div class="table-responsive">
        <table class="table table-bordered table-striped">
                    <td>Contact No</td>
            @foreach (var item in Model)

 Now run your application



In my next post I’ll explain about Ajax with Asp.net MVC

  Modified On Nov-30-2017 03:46:36 AM

Leave Comment