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

Create Validation in form control MVC .NET web application

MS Dot NET 1115 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

What is Bounce Rate in SEO? Complete Guide for Beginners
search-engine-optimization
What is Bounce Rate in SEO? Complete Guide for Beginners

Learn what bounce rate is in SEO, how it is calculated, why it matters, common causes of high bounce rates, an...

👁 28 2026-05-24
Read More →
Comprehensive Interviewer Guide - Detailed Article
skill
Comprehensive Interviewer Guide - Detailed Article

Learn how to conduct effective interviews with this comprehensive interviewer guide. Explore hiring strategies...

👁 43 2026-05-22
Read More →
Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)
skill
Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)

Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)...

👁 38 2026-05-19
Read More →
How to Grow Your Business Mindset Step by Step
skill
How to Grow Your Business Mindset Step by Step

Learn how to develop and grow a successful business mindset step by step. Discover entrepreneurial thinking, p...

👁 56 2026-05-09
Read More →