1

I tried to do the log in form in my project.

Here is my Controller :

 public ActionResult Index()
 {
    return View();
 }
 [HttpPost] 
 public ActionResult Index(UserModels model)
 {
     if (ModelState.IsValid)
     {
        if (model.IsValid(model.UserName, model.Password))
        {
          FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
          return RedirectToAction("Introduction", "Home");
        }
        else
        {
          ModelState.AddModelError("", "The user name or password provided is incorrect.");
        }
    }
    return View(model);
 }

And here is my model :

 [Required(ErrorMessage = "*")]
 public string UserName { get; set; }
 [Required(ErrorMessage = "*")]
 public string Password { get; set; }
 [Display(Name = "Remember me?")]
 public bool RememberMe { get; set; }

 public bool IsValid(string _username, string _pwd)
 {
    EMP context = new EMP();
    var _userLogin = from u in context.tblEmployees
                     where u.UserName == _username &&
                     u.Password == _pwd
                     select u;
   if (_userLogin != null)
   {
      return true;
   }
   else
   {
      return false;
   }
 }

This is my Views :

<div>
  <% using (Html.BeginForm()) { %>
    <div style="position:relative; top:302px; vertical-align:middle;">
      <%: Html.TextBoxFor(m => m.UserName, new { @id = "txtUsername", size = "25" })%>
      <%: Html.ValidationMessageFor(m => m.UserName)%>
    </div>

    <div>
      <%: Html.PasswordFor(m => m.Password, new { @id = "txtPassword", size = "25" })%>
      <%: Html.ValidationMessageFor(m => m.Password) %>
    </div>

    <div>
      <input id="btnLogin" type="submit" value="LOGIN" />
    </div>
    <div style="position:relative; top:415px; vertical-align:middle;">
      <%: Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.")%>
    </div>
   <% } %>
</div>

But when I entered the valid user name and password in my view, then press button submit, debugging the ModelState.IsValid is always false.

Any one have any idea about this problem? Please share.

Thanks.

tereško
  • 58,060
  • 25
  • 98
  • 150
Nothing
  • 2,644
  • 11
  • 64
  • 115

2 Answers2

3

Not sure What is the reason for your error. But in such kind of scenario,for debugging i write an ELSE part and check what is the Model Error by checking the ViewData.ModelState.Values collection.

if (ModelState.IsValid)
{    
   //Do whatever you want with the valid Model    
}
else
{
    // Let's inspect what error message it is
   foreach (var modelStateValue in ViewData.ModelState.Values)
   {         {
      foreach (var error in modelStateValue.Errors)
      {
         //Use breakpoints and Let's check what it is in these properties
          var errorMessage = error.ErrorMessage;
          var exception = error.Exception;
      }
   }
}
Shyju
  • 214,206
  • 104
  • 411
  • 497
1

Is it from you manually setting the IDs of the tetboxes?

TextBoxFor rendering to HTML with prefix on the ID attribute

<%: Html.TextBoxFor(m => m.UserName, new { @id = "txtUsername", size = "25" })%>

Try without the "@id = "txtUsername"".

Community
  • 1
  • 1
Meff
  • 5,889
  • 27
  • 36