Modelstate.addmodelerror未显示在PartialView中

时间:2017-02-11 11:35:54

标签: c# asp.net-mvc-5 asp.net-identity-2

addmodelerror正在处理fr视图但不在部分视图中。

我的代码:  局部视图" _login"在视图Login.cshtml中调用:

@model MODEL.Identity.LoginViewModel

cost = tf.reduce_mean(tf.squared_difference(y, pred))

显示电子邮件和密码的错误消息,但不会显示我的ModelState.AddModelError(""," Nom d' utilisateur ou mot de passe non valide。" ); 行动准则:

@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">............

帮助谢谢

1 个答案:

答案 0 :(得分:0)

它解决了它有效,但我不知道这是不是很好的做法? 这是我的代码

[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.");  
    }

和我的观点或部分观点

    @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">............ 

如果您有任何其他想法,请发布