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:

using System;

using System.Collections.Generic;

using System.ComponentModel.DataAnnotations;

using System.Linq;

using System.Web;

 

namespace RangeValidationAPI.Models

{

    public class ClientModel

    {

        [Required(ErrorMessage = "Pleasee enter you number")]

        [Range(0, 1000, ErrorMessage = "Enter number between 0 to 1000")]

        public int NumberofClient { get; set; }

    }

}

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:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using RangeValidationAPI.Models;

namespace RangeValidationAPI.Controllers

{

    public class HomeController : Controller

    {

        public ActionResult Index()

        {

            return View();

        }

        [HttpPost]

        public ActionResult Index(ClientModel objclient)

        {

            if (ModelState.IsValid)

            {

                ViewBag.corrent = "Now The number is in Range";

            }

            return View();

        }

    }

}

 

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:

@model RangeValidationAPI.Models.ClientModel

@{

    ViewBag.Title = "Index";

}

@using (Html.BeginForm("Index", "Home"))

{

    <fieldset>

        <h2>

            Range Validation</h2>

        @Html.TextBoxFor(m => m.NumberofClient)

        @Html.ValidationMessageFor(m => m.NumberofClient)

        <p>

        <input type="submit" value="Submit" />

        </p>

        @ViewBag.corrent

    </fieldset>

}

Step5

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

Up Next
    Ebook Download
    View all
    Learn
    View all