Set Validation in an ASP.NET MVC Application


Introduction: In this article you will learn how to set the validation in an ASP.NET MVC application. In this program we simple display four fields, the name of the fields are Id, Name, Discreption, Prise. First we have to create the business logic in the models folder. After that we send the request through a controller. Last, we define the presentation logic. The ASP.NET MVC framework was created to support pattern-based software development. In other words, the framework was designed to make it easier to implement software design principles and patterns when building web applications. ASP.NET MVC applications are highly testable makes the ASP.NET MVC framework a great framework to use when practicing test-driven development. ASP.NET MVC framework was designed to its core to support unit tests.

Step 1 : Open Visual Studio 2010.

  • Go to file -> New->Projects
  • Create an ASP.NET MVC 2  Web Application
  • Name is "sonal"
  • Create a unit test
stqarat.gif

strrrrrrrrrrrrrr.gif

Step 2 : Add a class in the models folder.

  • Right click on the Model folder ->add new items->add class
  • Name of Class is "bhanu"
  • In a class define the business logic

model.gif

class.gif

Step 3 :  The class code is..

Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
namespace sonal.Models
{
    public class Bhanu
    {
        public int Id { get; set; }
        [Required]
        [StringLength(10)]
        public string Name { get; set; }
        [Required]
        public string Description { get; set; }|
        [DisplayName("Price")]
        [Required]
        [RegularExpression(@"^\$?\d+(\.(\d{2}))?$")]
        public decimal UnitPrice { get; set; }
    }
}

Step 4 : Add a controller.

  • Right click on the Controllers folder ->add->Controllers.
  • Name of Controllers is "Home".
  • In a controller, define the request.
controller.gif

Step 5 : The controller code is:

Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using sonal.Models;
namespace sonal.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";
            return View();
        }
        public ActionResult About()
        {
            return View();
        }
        public ActionResult Create()
        {
            ViewData["Message"] = "This is manish application";
            return View();
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "Id")]Bhanu productToCreate)
        {
            if (!ModelState.IsValid)
                return View();
return
RedirectToAction("Index");
        }
    }
}

Step 6 : Add the second view.

  • Name of view is "Create view"
view.gif

view-name.gif

view-design.gif

Step 7 : The view code is:

Code :

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<sonal.Models.Bhanu>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
       Create
</asp:Content>
<
asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2 style="background-color: #FF66FF">Manish</h2>
    <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>
        <fieldset>
            <legend style="background-color: #FF9999">Fields</legend>
            <div class="editor-label">
                <%: Html.LabelFor(model => model.Id) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Id) %>
                <%: Html.ValidationMessageFor(model => model.Id) %>
            </div>
            <div class="editor-label">
                <%: Html.LabelFor(model => model.Name) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Name) %>
                <%: Html.ValidationMessageFor(model => model.Name) %>
            </div>
            <div class="editor-label">
                <%: Html.LabelFor(model => model.Description) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Description) %>
                <%: Html.ValidationMessageFor(model => model.Description) %>
            </div>
            <div class="editor-label">
                <%: Html.LabelFor(model => model.UnitPrice) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.UnitPrice) %>
                <%: Html.ValidationMessageFor(model => model.UnitPrice) %>
            </div>
            <p style="background-color: #FFFF00">
                <input type="submit" value="Create" style="background-color: #8080FF" />
            </p>
        </fieldset>
    <% } %>
    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>
</asp:Content>

Step 8 :  Press crlt+f5 run the program.

Output :

outputtttttttttttttttt.gif

ddddddddddddddd.gif

Up Next
    Ebook Download
    View all
    Learn
    View all