简体   繁体   English

未在PartialView中显示Modelstate.addmodelerror

[英]Modelstate.addmodelerror not showing in PartialView

addmodelerror isworking fr views but not in partial views. addmodelerror正在工作,但不能在部分视图中使用。

My Code: Partial view "_login" which is called in view Login.cshtml : 我的代码:在视图Login.cshtml中调用的局部视图“ _login”:

@model MODEL.Identity.LoginViewModel @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."); “;显示电子邮件和密码的错误消息,但不显示我的ModelState.AddModelError(”“,”无效的密码。“); 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 帮助谢谢

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 如果您有其他想法,请发布

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ModelState.AddModelError 这里我给出的错误消息没有显示 - ModelState.AddModelError here the error message I gave its not showing 创建一个类型化的ModelState.AddModelError() - Creating a typed ModelState.AddModelError() 更改 ModelState.AddModelError 消息的字体颜色 - Changing the FONT color of the ModelState.AddModelError message 模型状态。 AddmodelError没有显示 - Modelstate. AddmodelError is not showing 添加ModelState.AddModelError而无需重新加载页面(mvc4) - Add ModelState.AddModelError without reload the page (mvc4) ASP.NET MVC 2 RC在表单级别上的ModelState.AddModelError - ModelState.AddModelError on form level with ASP.NET MVC 2 RC 当MVC4中的模型为空时如何显示ModelState.AddModelError - How to show ModelState.AddModelError when the Model is Empty in MVC4 通过javascript提交表单后将ModelState.AddModelError添加到视图中 - Add ModelState.AddModelError to the view after submitting form through javascript ModelState.AddModelError 但没有 ErrorMessage(甚至是默认值) - 仅突出显示该字段 - ModelState.AddModelError but without the ErrorMessage (even default) - only highlight the field 未绑定模型项时如何添加 ModelState.AddModelError 消息 - How to add ModelState.AddModelError message when model item is not binded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM