views:

527

answers:

1

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.

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.

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.

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

[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  A: 

You can return JsonResult from RetireSelf action:

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;
    }
});
eu-ge-ne
Using this method though, how do handle redirecting to another page in the same operation? Wouldn't that JSON data not be available on the next page? Unless I pass it in via the query string, which obviously I want to avoid doing.
Chops
Because what I ultimately want to do is perform the action, navigate off the page, and display the notification message. I can display the notification, I can handle the page redirect, but seemingly not the two in the one operation.
Chops
You are setting TempData on POST also. On the next request the information from TempData should be present. Post the code from that 'next page' action - maybe the problem in that code? How are you reading TempData in 'next page'?
eu-ge-ne
Ahhhh, good suggestion. I've spotted what was wrong. Idiot me missed the fact that I have an interim action that does a redirect based on some parameters, so the TempData was being nulled because of that interim request. That works great now. Thanks for your help!Although it does still leave me with a question in passing interest.... if you call Actions this way, there's no easy way to have them do the page redirection themselves, you have to handle it client side, which seems less than ideal, doesn't it?
Chops
I'm using redirecting by setting windows.location on client side too, I dont think that something is wrong with this solution
eu-ge-ne