Create Validation in form control MVC .NET web application

Rumman Ansari   2019-03-13   Student   MS dot NET > Create-dropdown-list-using-mvc-dot-net   9797 Share

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>