Home > DeveloperSection > Articles > Working With Model in MVC4

Working With Model in MVC4


ASP.NET MVC Mvc4  Model 
Ratings:
0 Comment(s)
 1696  View(s)
Rate this:

Working With Model in MVC4

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

Description:

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.

<connectionStrings>

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

  </connectionStrings>

 

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

@model IList<StudentManagement.Models.StudentDetails>

@{

    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">

            <thead>

                <tr>

                    <td>ID</td>

                    <td>Name</td>

                    <td>EmailId</td>

                    <td>Address</td>

                    <td>Contact No</td>

                </tr>

            </thead>

            @foreach (var item in Model)

            {

                <tr>

                    <td>@item.ID</td>

                    <td>@item.Name</td>

                    <td>@item.EmailId</td>

                    <td>@item.Address</td>

                    <td>@item.ContactNo</td>

                </tr>

            }

        </table>

    </div>

</div>

 

Now run your application

Output:

 

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


Don't want to miss updates? Please click the below button!

Follow MindStick