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

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 

         In MVC



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)




Token Based Authentication for Web API's

Securing ASP.NET Web API using Custom Token Based AuthenticationProviding a security to the Web API&...

Read More >

ASP.NET: Audit Trail Implementation using Entity Framework

When you are working with certain projects which involves the Customer records, you might need to tr...

Read More >

Asynchronous actions in MVC 5

What does it mean?Asynchronous actions allow developers to handle more concurrent requests and can b...

Read More >

ELMAH Integration in ASP.NET MVC Application

ELMAH(Error Logging Modules And Handlers)What is ELMAH?ELMAH (Error Logging Modules and Handlers) is...

Read More >

Unit testing in .NET

Unit Testing – ExplainedA process which involves writing code to verify a system at a lower an...

Read More >


View Engine is responsible for rendering the view into html form to the browser.ASP.NET MVC includes...

Read More >

ViewData and ViewBag in MVC

ASP.NET MVC provides us two options ViewData and ViewBag for passing data from controller to view.Vi...

Read More >

Brief Journey to Asp.Net MVC Framework

Introduction: ASP.NET MVC is a  framework for building web applications that uses the mode...

Read More >


Try DevOpSmartBoard Ultimate complete Azure DevOps End-to end reporting tool

Sign Up

  • Recent
  • Popular
  • Tag
Monthly Archive

Contact Us
  • *
  • *