Amit Patel
Amit Patel

Reputation: 69

Require field validator is not working in ASP.NET MVC

Following is my code and I don't know why my validator is not working First code is User model then second is home controller and last is view of the Index action result.I am new to MVC.COuld you please help me. Thanks

    public class User
    {
        [Required(ErrorMessage = "UserName is required")]
        public string UserName { get; set; }

        [Required(ErrorMessage = "Password is required")]
        public string Pasword { get; set; }
    }

     public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(string uname,string pass)
        {
            if(ModelState.IsValid)
            { 
             User user = new User();
            user.UserName = uname;
            user.Pasword = pass;
            string Username = ConfigurationManager.AppSettings["username"].ToString();
            string passwrd = ConfigurationManager.AppSettings["password"].ToString();
            if (user.UserName !=null && user.Pasword !=null)
             {
                 if(user.UserName==Username && user.Pasword==passwrd)
                {
                    return RedirectToAction("Detail", "Home");
                }                            
             }  
           }
            return View("Index");
        }


<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.Pasword, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Pasword, new { htmlAttributes = new { @class = "form-control" } })              
                @Html.ValidationMessageFor(model => model.Pasword, "", new { @class = "text-danger" })
            </div>
        </div>

Upvotes: 0

Views: 1112

Answers (1)

Usman
Usman

Reputation: 4703

first of all you should send model instead of individual properties like

[HttpPost]
        public ActionResult Index(User user)
        {
            if(!ModelState.IsValid)
            { 
               return View("Index",user);
            }

            // your code here if model is valid
            return View("Index");
        }

it will check if ModelState is Invalid it will redirect to the same view with ModelState which will include key and value so if validation of a property fails its name will be the key and message will be in the value of the key validation messages will be populated according to their keys(property names)

Upvotes: 2

Related Questions