【问题标题】:Calling an ActionResult of Controller in Javascript在 Javascript 中调用控制器的 ActionResult
【发布时间】:2020-11-07 04:50:42
【问题描述】:

我正在尝试在 javascript 中调用 Controller 内的 ActionResult。

我的 AdminController 中有这个 ActionResult。

[HttpPost]
public ActionResult Logout()
{
    return RedirectToAction("Login", "Login");
}

这是我在 AdminView 中的Logout button

<a class="navbar-link" id="logout" name="logout" href="#">
    ログアウト
</a>

并尝试在 JAVSCRIPT 中创建一个事件。

  $("#logout").click(function () {
        swal({
            title: "ログアウト?",
            text: "アカウントからサインアウトしますか?",
            icon: "warning",
            buttons: true,
            dangerMode: true,
        })
            .then((willLogout) => {
                if (willLogout) {
                    //swal("Poof! Your imaginary file has been deleted!", {
                    //    icon: "success",
                    //});
                    $.ajax({
                        url: "/Admin/Logout",
                        type: 'POST',
                        success: function (result) {
                            document.location.reload(true);
                        }, 
                        error: function (result) {

                        }
                    });
                }
            });
    });

这样做的主要目的是使用控制器将用户重定向到登录页面。

我尝试将swal("Poof")... 设置在.then(willLogout) 中,它可以工作。

但是当我使用 ajax 调用 ActionResult 时。它没有用。

我看到了控制台,似乎什么也没显示。

我不知道我做错了什么。

如何通过Ajax 调用从Controller 调用ActionResult 到Javascript 文件?

【问题讨论】:

  • 你用的是什么框架?
  • @HugoElhaj-Lahsen ASP.NET MVC

标签: javascript ajax model-view-controller


【解决方案1】:

好吧,帮助我的初学者。这是我应付的解决方案。

首先,我了解到每个 ActionResult 都应该有一个 View(如果我没记错的话)。

所以,在我的AdminView.cshtml 中,我创建了一个隐藏按钮。

  <button type="submit" id="submitLogout" name="button" value="logout" hidden>logout</button>

之后,在我的JSFILE

我删除了 ajax。这首先是错误的。

   $("#logout").click(function () {
        swal({
            title: "ログアウト?",
            text: "アカウントからサインアウトしますか?",
            icon: "warning",
            buttons: true,
            dangerMode: true,
        })
            .then((willLogout) => {
                if (willLogout) {
                    $("#submitLogout").click();
                }
            });
    });

javascript中的这个函数是触发.cshtml中的隐藏按钮,触发Controller中的ActionResult

另外,在我的控制器中。这就是我所说的每个ActionResult 必须有一个View。

 public ActionResult AdminView(string button)
    {
        if (button == "logout")
        {
            FormsAuthentication.SignOut();
            Session.RemoveAll();
            Session.Abandon();
            return RedirectToActionPermanent("Login", "Login");
        }
        else
        {
            if (!(Session["UserInfo"] is UserModel))
            {
                return RedirectToActionPermanent("Login", "Login");
            }
            else
            {
                return View();
            }
        }
    }

ActionResult中的参数string button就是你要触发的name of the button。如果触发成功,它会将按钮的值传递给参数string button

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    • 2018-01-27
    • 2020-07-06
    相关资源
    最近更新 更多