Search code examples
c#asp.net-mvc-5asp.net-identity-2

Modelstate.addmodelerror not showing in PartialView


addmodelerror isworking fr views but not in partial views.

My Code: Partial view "_login" which is called in view Login.cshtml :

@model MODEL.Identity.LoginViewModel

@using (Ajax.BeginForm("Login", "MyAccount", new AjaxOptions { HttpMethod = "POST"}, new { role = "form" }))
{

   @Html.AntiForgeryToken()
        <div class="main-container">
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })


        <div class="container">............

The error messages of email and password are displayed but not for my ModelState.AddModelError("", "Nom d'utilisateur ou mot de passe non valide."); Code in Action:

 [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {

            ApplicationDbContext context = new ApplicationDbContext();              
            var u = await UserManager.FindByEmailAsync(model.Email);
            bool passhash = false;
            if (u != null)
            {
                passhash = await UserManager.CheckPasswordAsync(u, model.Password);
            }

            if (u != null && passhash)
            {
                await SignInAsync(u, model.RememberMe);
                 return RedirectToLocal(returnUrl);
            }
            else
            {
                ModelState.AddModelError("", "Nom d'utilisateur ou mot de passe non valide.");
            }

        }

        return PartialView("~/views/MyAccount/_login.cshtml",model);

    }

Help Thanks


Solution

  • Its resolved it works but i dont know if it is good practice? this is my code

    [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<string> Login(LoginViewModel model )
        {                 
                var u = await UserManager.FindByEmailAsync(model.Email);
                bool passhash = false;
                if (u != null)
                {
                  passhash = await UserManager.CheckPasswordAsync(u, model.Password);
                }                
                if (u != null && passhash)
                {
                  await SignInAsync(u, model.RememberMe);                 
                  return ("ok");
                }
                else                
                   return ("Nom d'utilisateur ou mot de passe non valide.");  
        }
    

    and my view or partial view

        @model MODEL.Identity.LoginViewModel
    <script type="text/javascript">
        function OnSuccess(response) {       
            if (response != "ok") 
            {           
                document.getElementById("errormessage").innerHTML = response
            }
            else 
            {
                window.location.href = "/Home/Index/";
            }
        }
        </script>
    
         @using (Ajax.BeginForm("Login", "MyAccount", new AjaxOptions { OnSuccess = "OnSuccess" }, new { @class = "form-horizontal", role = "form" }))
      {
        <div id="errormessage" class='field-validation-error text-danger' style=" display: block;"></div>
             @Html.AntiForgeryToken()
            <div class="main-container">
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    
                <div class="container">............ 
    

    If you have any other idea, post it