In my previous article Introduction to Asp.Net MVC, we discussed what Asp.Net MVC and its advantages. In this article, we discuss what Model is and how to create Model.
Model is nothing but just C# class. If you know how to create C# class, that means you know already how to create a Model. In Model, we can have code to connect to the database, calling web service, call another project to get the data,…etc. The model class contains all related to data. We can have certain business logic to perform on data, and we can have data validations also in the model.
In general, Model class contains properties based on data. For example, if your model class related to Employee, then it can contain properties like EmployeeId, EmployeeName, EmployeeEmail, EmployeeSalary,….., etc. We can apply validations on these properties with the help of properties. We can maintain centralized validations by implementing the validation rules on model class because we can pass this model to View to display the data or we can pass this model to controller also. That means by applying validations on Model; we are applying validations on all parts of our application.
There are different types of attributes available to apply on a model class. Those are
DataTypeAttribute
DisplayAttribute
Validation Attributes
DataTypeAttribute: While creating the properties for a model class we can mention data types for them. For example, if you have email address property, by applying DataType.EmailAddress attribute you can render it as type=”email” in View. We discuss this with some example later in this article.
There are seventeen types of DataType Attributes. Those are
Custom
DateTime
Date
Time
Duration
PhoneNumber
Currency
Text
Html
MultilineText
EmailAddress
Password
Url
ImageUrl
CreditCard
PostalCode
Upload
By applying above attributes, we can control input data.
DisplayAttribute: By applying the DisplayAttribute we can change the input controls display properties like name, description,.., etc. There are different type Display Attributes are there. Those are
Description
GroupName
Name
Order
Prompt
ResourceType
ShortName
ValidationAttribute: With the help of ValidationAttribute, we can apply the validation rules for input data. There are four types of validation attributes and those are
RequiredAttribute
StringLengthAttribute
RegularExpressionAttribute
CompareAttribute
Now we discuss how we can apply all the above attributes with a simple example. Open Microsoft Visual Studio2015 => Create New Project, select Asp.Net Web Application and name it as “Ecommerce”. From now onwards we discuss Asp.Net MVC with the Ecommerce example. Select MVC from available templates and click OK as shown below.
Right click on Models folder in the Solution Explorer and add a new class with the name as “Product”. Add different properties and apply different attributes for the Product class as shown below.
using System.ComponentModel.DataAnnotations;
namespace Ecommerce.Models
{
public class Product
{
public int ProductId { get; set; }
[StringLength(10)]
[Display(Name = "Product Name")]
public string ProductName { get; set; }
[DataType(DataType.EmailAddress)]
[Display(Name = "Merchant Email Address")]
public string MerchantEmail { get; set; }
[DataType(DataType.Currency)]
[Display(Name = "Product Price")]
public double ProductPrice { get; set; }
}
}
As shown above, we have applied StringLength attribute to restrict the data length for Product Name, DataType.EmailAddress to allow only email address, DataType.Currency to allow only valid currency. We also applied Display attributes to change the name and description values for input controls.
Build the project and add Controller. Open Solution Explorer, right click on Controllers folder and add a new controller. Select option as “MVC 5 Controller with views, using Entity Framework” as shown below.
We will discuss more about Controllers, Views and Entity Framework in my future articles.
Select Model Class as Product, Data context class as ApplicationDbContext as shown below and click Add.
Here MVC engine given the controller name as “ProductsController”, if you want you can change the name of the controller. Run the application and modify the URL to http://localhost:58377/Products (here 58377 is my local port, it can be anything for your project). It displays the output as shown below.
Click on Create New; it shows the screen to add new product as shown below.
If you enter wrong values for Product Name or Email Address or Product Price, it displays the error messages as shown below.
This is because we have applied the data validation rules by using attributes on Product model class. In my next article, we discuss more on Asp.Net Models with the help of examples.