Create Validation in form control MVC .NET web application | YourSite

Create Validation in form control MVC .NET web application

MS Dot NET 1127 views

In this tutorial we are going to to see how you can validate your form through the MVC .net web aplication

So late's start

View Page


@model MVCAssetManagementSystem.Models.aa_LoginMaster_1637935

@{
    ViewBag.Title = "Login";
}

<h2>Login</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>aa_LoginMaster_1637935</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @*@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })*@
                @Html.PasswordFor(model=> model.Password, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
     

}


<div style="background-color:orange">
    @ViewBag.Message
</div>


<div>
    @Html.ActionLink("Back to List", "Index")
</div>

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>


Login Meta page


using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace MVCAssetManagementSystem.Models
{
    public class LoginMetaData
    {

        [Required(ErrorMessage = "Please Enter User Name")]
        public string UserName { get; set; }
        [Required(ErrorMessage = "Please Enter Password")]
        public string Password { get; set; }
    }

    [MetadataType(typeof(LoginMetaData))]
    public partial class aa_LoginMaster_1637935 {


    }
}

Code inside Controller

Here if(ModelState.IsValid) is very much important


 [HttpPost]
        public ActionResult Login(aa_LoginMaster_1637935 obj)
        {

            if (ModelState.IsValid)
            {
                aa_LoginMaster_1637935 username = new aa_LoginMaster_1637935();
                string name = "admin";
                username = dbObject.aa_LoginMaster_1637935.Where(x => x.UserName == name).FirstOrDefault();

                if (obj.UserName == username.UserName && obj.Password == username.Password)
                {
                    return RedirectToAction("AddAsset");

                }
                else
                {
                    ViewBag.Message = "Sorry !! Wrong Credential";
                    return View();
                }
            }
            else
                return View();
            
             
                      
        }


Create a list inside view for your drop downlist

In view page


@{

    List<SelectListItem> list1 = new List<SelectListItem>();
    list1.Add(new SelectListItem
    {
        Text = "Laptop",
        Value = "Laptop",
    });
    list1.Add(new SelectListItem
    {
        Text = "Desktop",
        Value = "Desktop",
    });
}

This is inside the same page

@Html.DropDownListFor(model => model.AllocationStatus, list1) this line is important


    <div class="form-group">
            @Html.LabelFor(model => model.AllocationStatus, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                // @Html.EditorFor(model => model.AllocationStatus, new { htmlAttributes = new { @class = "form-control" } })
                @Html.DropDownListFor(model => model.AllocationStatus, list1)
                @Html.ValidationMessageFor(model => model.AllocationStatus, "", new { @class = "text-danger" })
            </div>
        </div>

🚀 More Blogs You Might Like

Explore more articles and keep learning

Data Science Roadmap: From Complete Beginner to Job-Ready Practitioner
Data-Science
Data Science Roadmap: From Complete Beginner to Job-Ready Practitioner

Data Science Roadmap: From Complete Beginner to Job-Ready Practitioner...

👁 8 2026-07-26
Read More →
How AI Is Changing the Way Software Is Made
Data-Science
How AI Is Changing the Way Software Is Made

How AI Is Changing the Way Software Is Made...

👁 7 2026-07-26
Read More →
8 Mistakes That Quietly Slow Down Talented Developers
Personal-Development
8 Mistakes That Quietly Slow Down Talented Developers

8 Mistakes That Quietly Slow Down Talented Developers...

👁 29 2026-07-12
Read More →
Does religion teach hatred?
islam
Does religion teach hatred?

It is easy to spread hatred in the name of religion, but understanding the true message of religion is much de...

👁 319 2026-06-30
Read More →