Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
    Single Page App
    Bootstrap
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

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


by Rohit Kesharwani on 9/30/2011 2:00:04 PM

Views: 4143       Comments: 0

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.

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Rohit KesharwaniRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 5422
Advertisement
MindStick DataConver
Advertise with Us
  
Copyright © 2009 - 2014MindStick. All Rights Reserved.