在没有AJAX的情况下从JQuery UI Dialog调用MVC方法

时间:2021-02-14 19:47:17

I currently make use of JQuery UI dialogs across my site. However, I've recently started to make use of the MVC TempData collection for notifications of success/failure of various actions triggered by my dialogs. Nothing particularly fancy or new. However, it's brought up an issue that I can't find a simple, obviously solution to.

我目前在我的网站上使用JQuery UI对话框。但是,我最近开始使用MVC TempData集合来通知我的对话框触发的各种动作的成功/失败。没什么特别的花哨或新的。然而,它提出了一个我无法找到一个简单明显的解决方案的问题。

On some of my dialogs, we redirect to a new page on successful submission of the data. This data is being submitted using jQuery.Post, and then we do the redirect on successful submission using window.location on the page. However, this means that any TempData we set in the controller method isn't available. Makes sense, as it seems to require the ActionResult return type to handle this.

在我的一些对话框中,我们在成功提交数据时重定向到新页面。这个数据是使用jQuery.Post提交的,然后我们使用页面上的window.location成功提交重定向。但是,这意味着我们在控制器方法中设置的任何TempData都不可用。有道理,因为它似乎需要ActionResult返回类型来处理这个问题。

So, my question was, using JQuery UI Dialogs, what would people suggest as a way to submitting data to the controller WITHOUT using jQuery POST or AJAX calls. Obviously I can embed a form within the dialog myself and use that, but it seems to half-defeat the point of using JQuery UI Dialog when it handles all the buttons, etc, for you.

所以,我的问题是,使用JQuery UI Dialogs,人们会建议将数据提交给控制器而不使用jQuery POST或AJAX调用。显然我可以自己在对话框中嵌入一个表单并使用它,但是当它为你处理所有按钮等时,它似乎有一半失败了使用JQuery UI Dialog。

Perhaps I'm missing something really obvious, but any help would be greatly appreciated. Many thanks.

也许我错过了一些非常明显的东西,但任何帮助都会非常感激。非常感谢。

Updated: Here is the entire Action method. NB - this is one implementation, I've actually tried this several ways. But this is the current implementation. Once the Action is called, and we do a client side redirect, the TempData I've set comes out as NULL

更新:这是整个Action方法。注意 - 这是一个实现,我实际上尝试过这几种方式。但这是当前的实施。一旦调用了Action,并且我们进行了客户端重定向,我设置的TempData就会显示为NULL

[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
    public void RetireSelf(int playerKey)
    {
        PlayerDTO player = _playerTask.GetPlayer(playerKey);

        _log.Info("Player retiring themselves from ladder " + player.Name + player.PlayerKey);

        UserDTO user = _userTask.GetUser(CurrentUserName);

        if (user.UserKey != player.UserKey)
        {
            throw new LadderSecurityException(CurrentUserName + "trying to self retire another player");
        }

        _playerTask.RetirePlayer(playerKey);
        TempData["notification"] = "You were retired from the ladder.";
    }

1 个解决方案

#1


1  

You can return JsonResult from RetireSelf action:

您可以从RetireSelf操作返回JsonResult:

if(Request.IsAjaxRequest())
{
    return Json(new { Notification = "You were retired from the ladder."});
}

On client:

$.ajax({ url: ..., type: "POST", data: ..., dataType: "json",
    success: function(data) {
        var notification = data.Notification;
    }
});

#1


1  

You can return JsonResult from RetireSelf action:

您可以从RetireSelf操作返回JsonResult:

if(Request.IsAjaxRequest())
{
    return Json(new { Notification = "You were retired from the ladder."});
}

On client:

$.ajax({ url: ..., type: "POST", data: ..., dataType: "json",
    success: function(data) {
        var notification = data.Notification;
    }
});