Home > DeveloperSection > Articles > Model in ASP.NET MVC

Model in ASP.NET MVC

Posted by  Rohit Kesharwani
on    September-30-2011 14:00 PM

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

Model in ASP.NET MVC

MVC model is basically a C# or VB.NET class. A model is accessible by both controller and view. A model can be used to pass data from controller to view. A view can use model to display data in page.

We can add Model as depicted in a figure below:

Right Click on Model Folder à Add à Class.

Model in ASP.NET MVC

After selecting Class option, change the class name if you want. Here I change it into ModelClass.cs
Then click on Add button.

Model in ASP.NET MVC

After adding a class, you must also have to know the functionality of that class. In Model class we can implements properties, methods and validations attribute etc. according to our need. We can perform all the programming operation in the Class as we did earlier in various programming languages.

In this class I had created two properties i.e. FirstName and LastName. At the above of both property I also created various (Required, StringLength, RegularExpression) attribute that put various validation on properties.
Instead of properties and methods I also create a boolean method named IsExist that returns either true or false when invoked.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.ComponentModel;

using System.ComponentModel.DataAnnotations;

 

namespace FisrtMVCApp.Models

{

    public class ModelClass

    {

        [Required(ErrorMessage="First Name Required:")]

        [DisplayName("Enter First Name:")]

        [StringLength(30, ErrorMessage = ":Less than 30 characters")]

        [RegularExpression(@"^[a-zA-Z'.\s]{1,40}$", ErrorMessage = "Special Characters 
                                                                        not allowed"
)]

        public string FirstName

        {

            get;

            set;

        }

 

        [Required(ErrorMessage = "First Name Required:")]

        [DisplayName("Enter Last Name:")]

        [StringLength(30, ErrorMessage = ":Less than 30 characters")]

        [RegularExpression(@"^[a-zA-Z'.\s]{1,40}$", ErrorMessage = "Special Characters
                                                                        not allowed"
)]

        public string LastName

        {

            get;

            set;

        }

 

        public bool IsExist()

        {

            return false;

        }

    }

 

By creating above Model class, we can understand and can easily implement the functionality of Model in MVC ASP.NET.
















Recent Activities


Kamlakar Singh added new Blog Convert Text Document to PDF File   8 days ago

Kamlakar Singh added new Article Introduction to Backbone.js   8 days ago

Kamlakar Singh added new Question Explain the AdRotator Control.   8 days ago

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