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"; } Login
@using (Html.BeginForm()) { @Html.AntiForgeryToken()}aa_LoginMaster_1637935
@Html.ValidationSummary(true, "", new { @class = "text-danger" })@Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })@Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })@*@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" })@ViewBag.Message@Html.ActionLink("Back to List", "Index")
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 list1 = new List();
list1.Add(new SelectListItem
{
Text = "Laptop",
Value = "Laptop",
});
list1.Add(new SelectListItem
{
Text = "Desktop",
Value = "Desktop",
});
}
This is inside the same page
In view page
@{ List list1 = new List (); 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
@Html.LabelFor(model => model.AllocationStatus, htmlAttributes: new { @class = "control-label col-md-2" })// @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" })