Range Validation in ASP.Net MVC4 Web API

Introduction

This article explains Range Validation in the ASP.Net MVC4 Web API. Range Validation applies to ensuring that the user input value is within the specified range. We can use Range Validation on both strings and numbers. He will use Range Validation on the number.

Use the following procedure to create a sample application.

Step 1

  • Start Visual Studio 2013.
  • From the Start Window select "New Project".
  • From the New project window select "Installed" -> "Visual C#" -> "Web" -> "Visual Studio 2012".
  • Select "ASP.NET MVC4 Web Application" and click the "OK" button.

    Select MVC4 Application
  • From the "MVC4 project" window select "Web API".

    Select Web API
  • Click on the "OK" button.

Step 2

Add a Model folder as in the following:

  • In the "Solution Explorer".
  • Right -click on the "Model folder".
  • Select "Add" -> "Class".
  • From the add item window select "Installed" -> "Visual C#".

    Add Model Class
  • Select "Class" and click on the "Add" button.

Add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. using System.Linq;  
  5. using System.Web;  
  6. namespace RangeValidationAPI.Models  
  7. {  
  8.     public class ClientModel  
  9.     {  
  10.         [Required(ErrorMessage = "Pleasee enter you number")]  
  11.         [Range(0, 1000, ErrorMessage = "Enter number between 0 to 1000")]  
  12.         public int NumberofClient { getset; }  
  13.     }  
  14. } 

Step 3

Now in the "Controller" we add the code that uses all the variables of the Model class. This file exists:

  • In the "Solution Explorer".

  • Expand the "Controller" folder.

  • Select the "HomeController".

    Controller

Add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using RangeValidationAPI.Models;  
  7. namespace RangeValidationAPI.Controllers  
  8. {  
  9.     public class HomeController : Controller  
  10.     {  
  11.         public ActionResult Index()  
  12.         {  
  13.             return View();  
  14.         }  
  15.         [HttpPost]  
  16.         public ActionResult Index(ClientModel objclient)  
  17.         {  
  18.             if (ModelState.IsValid)  
  19.             {  
  20.                 ViewBag.corrent = "Now The number is in Range";  
  21.             }  
  22.             return View();  
  23.         }  
  24.     }  
  25. } 

Step 4

In the View write some code as in the following:

  • In the "Solution Explorer".

  • Expand the "Views Folder"

  • Select "Home" -> "Index.cshtml".

Add the following code:

  1. @model RangeValidationAPI.Models.ClientModel  
  2. @{  
  3.     ViewBag.Title = "Index";  
  4. }  
  5. @using (Html.BeginForm("Index""Home"))  
  6. {  
  7.     <fieldset>  
  8.         <h2>  
  9.             Range Validation</h2>  
  10.         @Html.TextBoxFor(m => m.NumberofClient)  
  11.         @Html.ValidationMessageFor(m => m.NumberofClient)  
  12.         <p>  
  13.         <input type="submit" value="Submit" />  
  14.         </p>  
  15.         @ViewBag.corrent  
  16.     </fieldset>  
  17. } 

Step 5

Now execute the application; the output will be as in the following:

Display Output Screen

Write any string in the TextBox. It will display a validation message.

Show validation for string

Now write a value outside of the range, then it will again display a validation message.

Value out of Range

Now write the value within range. Then it accepts it.

Right range


Similar Articles