【问题标题】:How to re-open modal window after ModalState is invalid with the errors shown?模态状态无效并显示错误后如何重新打开模态窗口?
【发布时间】:2018-06-18 21:30:57
【问题描述】:

我有一个具有验证功能的登录模式窗口。问题是验证失败时无法重新打开模态窗口。

所以,我有这个控制器:Index(),它返回主页的View。在该主页上,我有一个具有验证的模式窗口。
但是在单击提交按钮(Login() 控制器)后,在验证失败后,URL 是 /Index/Login 并且我无法传递 ViewBag 值,该值告诉模态重新打开。

这是两个控制器的代码:

public ActionResult Index()
 {
   return View();
 }

[HttpPost]
public ActionResult Login(Users user)
{
 if (ModelState.IsValidField("username") && ModelState.IsValidField("password"))
    {
      if (Users.Login(user))
          {       
           //some custom logic
           return RedirectToAction("../Dashboard/Dashboard");
          }
          else
          {
              ViewBag.LoginError = "Wrong username/password";
          }       
      }
      else
      {

      }
      return View("Index");
  }

我真的需要帮助。我整天都在努力解决这个问题!

PS:我在前面使用 Razor 语法。

【问题讨论】:

    标签: asp.net asp.net-mvc razor modal-dialog bootstrap-modal


    【解决方案1】:

    如果您在 /Views/Shared/_Layout.cshtml 中的 _Layout.cshtml 中有部分脚本,则将以下代码粘贴到 /Views/Home/Index.cshtml 的末尾。

    @section scripts
    {
        <script>
    
            @if(ViewBag.LoginError == "Wrong username/password")
            {
                WriteLiteral("$('#login-modal').modal('show');");
            }
        </script>
    }
    

    这也可以通过 ajax 方式完成,但是上面是您问题的解决方案。

    如果 _Layout.cshtml 中没有定义节脚本,则在 _Layout.cshtml 的末尾定义此节,如下所示。

    @RenderSection("scripts", required: false)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      • 2018-10-07
      • 1970-01-01
      相关资源
      最近更新 更多