blog

Home / DeveloperSection / Blogs / Create A Simple Model in Asp.Net MVC 4 Application

Create A Simple Model in Asp.Net MVC 4 Application

Sumit Kesarwani5569 29-Jan-2014

In this blog, I’m explaining how to create a simple model in asp.net mvc 4 application and how we can pass data using a model class.

Example

First create an empty asp.net mvc 4 application with razor engine in Visual studio 2012.

Adding a Model Class

In MVC, the M stands for model, and it is the most important part of the application. The model is the representation of the real-world objects, processes, and rules that define the subject, known as the domain, of our application. The model, often referred to as a domain model, contains the C# objects (known as domain objects) that make up the universe of our application and the methods that let us manipulate them. The views and controllers expose the domain to our clients in a consistent manner and a well-designed MVC application starts with a well-designed model, which is then the focal point as we add controllers and views.

Right click on the “Model Folder” in the solution explorer like this:



Create A Simple Model in Asp.Net MVC 4 Application


Click on the class option will open a pop-up like this:

Create A Simple Model in Asp.Net MVC 4 Application


Give the name “Student.cs” and click on Add button.

Visual studio will open the “Student.cs” file for editing:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SimpleModelApplication.Models
{
    publicclassStudent
    {
    }
}

Now add some properties in the Student.cs model class like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SimpleModelApplication.Models
{
    publicclassStudent
    {
        publicstring studentID { get; set; }
        publicstring studentName { get; set; }
        publicstring studentAddress { get; set; }
    }
}

Next Step is to add a controller named it “HomeController.cs” and write the below code in it:

using SimpleModelApplication.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace SimpleModelApplication.Controllers
{
    publicclassHomeController : Controller
    {
        //
        // GET: /Home/
        publicActionResult Index()
        {
            Student student = newStudent();
            student.studentID = "1001";
            student.studentName = "Steve Benson";
            student.studentAddress = "Washington DC";
            return View(student);
        }
    }
}

Now build the project.

Next step is to add a view to the project, right click on the index() in the HomeController.cs – a pop-up will open. Create a strongly-typed view and named it “Index.cshtml”.

 Write the below code in “Index.cshtml” file:

@model SimpleModelApplication.Models.Student
@{
    Layout = null;
}
<!DOCTYPEhtml>
 
<html>
<head>
    <metaname="viewport"content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <div>
        Student ID : @Model.studentID<br/>
        Student Name : @Model.studentName<br/>
        Student Address : @Model.studentAddress
    </div>
</body>
</html>

Output

Now run you application:-

Create A Simple Model in Asp.Net MVC 4 Application

Updated 18-Sep-2014

Leave Comment

Comments

Liked By