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

Create A Simple Model in Asp.Net MVC 4 Application


ASP.NET MVC ASP.NET MVC 
Ratings:
0 Comment(s)
 3444  View(s)
Rate this:

Create A Simple Model in Asp.Net MVC 4 Application

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:




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


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

{

    public class Student

    {

    }

}

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

{

    public class Student

    {

        public string studentID { get; set; }

        public string studentName { get; set; }

        public string 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

{

    public class HomeController : Controller

    {

        //

        // GET: /Home/

        public ActionResult Index()

        {

            Student student = new Student();

            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;

}

<!DOCTYPE html>

 

<html>

<head>

    <meta name="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:-


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

Follow MindStick