【问题标题】:Modelstate.addmodelerror not showing in PartialViewModelstate.addmodelerror 未显示在 PartialView 中
【发布时间】:2017-06-29 17:55:48
【问题描述】:

addmodelerror isworking fr views 但不在局部视图中。

我的代码: 在视图 Login.cshtml 中调用的部分视图“_login”:

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

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

 [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);

    }

帮忙谢谢

【问题讨论】:

  • 您正在更新的id="val123" 元素是什么?
  • @using (Ajax.BeginForm("Login", "MyAccount", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "val123" }, new { role = "表格" }))
  • 我的意思是@model MODEL.Identity.LoginViewModel using (Ajax.BeginForm("Login", "MyAccount", new AjaxOptions { HttpMethod = "POST"}, new { role = "form" }) ) { @Html.AntiForgeryToken()
    Html.ValidationSummary(true, "", new { class= "text-danger" })
  • 请编辑问题(不在 cmets 中)。您显示的代码将显示ModelState.AddModelError() 添加的错误,如果它到达该行。但是您的代码没有任何意义-您在控制器中有一个return RedirectToAction("Index", "Home");,但ajax 调用从不重定向(ajax 的全部意义在于保持在同一页面上。摆脱Ajax.BeginForm() 并使用Html.BeginForm() 使正常提交。
  • 而且您有一个从未使用过的string returnUrl 参数!正常行为是将用户返回到导航到的页面(即returnUrl - 而不是Index 视图)

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


【解决方案1】:

它解决了它的工作原理,但我不知道这是否是好的做法? 这是我的代码

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

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

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签