Canarys | IT Services


Creating a Simple Form with Validation Using ASP.NET MVC 4 Empty Template


There are multiple Validation attributes available in MVC. Validation attributes comes from System.ComponentModel.DataAnnotations.

Available validation attributes are-












Let us create a simple form to request a book using MVC4 basic template.

First step is to create the visual studio project.

1.  Open Visual Studio. Select New ➤ Project from the File menu.

2.  In the New Project window, expand C#, click Web, and then click the

ASP.NET MVC web Application

3. Give the project and solution a name, and then click OK.

4. In the New ASP.NET MVC 4 Project window, select the Empty

Template .

5. There are two types of View engines- ASPX view engine and Razor

View engine.

6. Select Razor view engine as it is the most commonly used view engine




Now the project has been created. We have to add the Home controller that will contain actions for the home page.

  1. Right click on the Controller node and click Add Controller

2. Name the controller as Home controller.

3. Click the Add button.

4. Add actions for RequestABook() and Thankyou().

5. Add an additional RequestABook() action and decorate it with HttpPost.




Creating a model.

1. Right click on models folder in solution explorer and click Add new class.

2. Change the name of the class to RequestABook.

3. Modify the Class to add Validations.

4. Add two important namespaces in order to enable validation that is System.ComponentModel and System.ComponentModel.DataAnnotations.



To add validations add suitable validation control to each feild in the model. Validation controls have several properties associated with them like-

ErrorMessage-Gets or sets an error message to associate with a validation control if validation fails.

ErrorMessageResourceName-Gets or sets the error message resource name to use in order to look up to  if validation fails.

ErrorMessageString-Gets the localized validation error message.

RequiresValidationContext-Gets a value that indicates whether the attribute requires validation context.

public class RequestABook
    [Required(ErrorMessage = "Name Required")]
    [DisplayName("Book Name")]
    public string BookName { get; set; }

    [Required(ErrorMessage = "ZipCode Required")]
    [DisplayName("Zip Code")]
    public string ZipCode { get; set; }

    [Required(ErrorMessage = "Contact Name Required:")]
    [DisplayName("Contact Name:")]
    [RegularExpression(@"^[a-zA-Z'.s]{1,40}$", ErrorMessage = "Special Characters   not allowed")]
    [StringLength(50, ErrorMessage = "Less than 50 characters")]
    public string ContactName { get; set; }

    [Required(ErrorMessage = "Email Id Required")]
    [DisplayName("Email ID")]
    ErrorMessage = "Email Format is wrong")]
    public string Email { get; set; }

   [Required(ErrorMessage = "Book Description Required")]
   [DisplayName("Book Description")]
   [StringLength(5000, MinimumLength = 10)]
   public string BookDescription { get; set; }


Creating a View.

1.In the Home Controller right click on Index method and Click on Add View.

2.To Create a strongly typed View  build the solution once and then Right Click on RequestABook method in the controller and Click Add view.

3.Then in the Dialogue Box check Create Strongly typed view and select RequestABook in the Dropdown.




Write the Following code in RequestABook.cshtml.



@using (Html.BeginForm())
Request Book
@Html.LabelFor(m => m.BookName)
@Html.EditorFor(m => m.BookName) @Html.ValidationMessageFor(m => m.BookName)
@Html.LabelFor(m => m.ZipCode)
@Html.EditorFor(m => m.ZipCode) @Html.ValidationMessageFor(m => m.ZipCode)
@Html.LabelFor(m => m.ContactName)
@Html.EditorFor(m => m.ContactName) @Html.ValidationMessageFor(m => m.ContactName)
@Html.LabelFor(m => m.Email)
@Html.EditorFor(m => m.Email) @Html.ValidationMessageFor(m => m.Email)
@Html.LabelFor(m => m.BookDescription)
@Html.EditorFor(m => m.BookDescription) @Html.ValidationMessageFor(m => m.BookDescription)


One response to “Creating a Simple Form with Validation Using ASP.NET MVC 4 Empty Template”

  1. I merely wanted to thank you yet again for your amazing web
    page you have designed here. Its full of ideas for those who are seriously interested in this particular subject,
    in particular this very post. Your all so sweet and also thoughtful of others and also reading your website
    posts is an excellent delight in my opinion. And what a
    generous gift! Mary and I will certainly have pleasure making use of your points in what we need to do
    next week. Our checklist is a mile long and tips will certainly be put to very
    good use.

Leave a Reply

Your email address will not be published. Required fields are marked *

Reach Us

With Canarys,
Let’s Plan. Grow. Strive. Succeed.