0

I am trying to display a the following div (via fadeIn/fadeOut) when a user saves data in a form.

// Display a notification if the save is successful.
$('<div class="save-alert">The current scenario has been saved.</div>')
    .insertAfter($('#main'))
    .fadeIn('slow')
    .animate({ opacity: 1.0 }, 3000)
    .fadeOut('slow', function () {
    $(this).remove();
});

Currently, I submitting the form using the Html.BeginForm helper. Because of this, my entire page is posted, and I cannot display the above notification. I realize I could use the Ajax.BeginForm, but then I run into other problems (namely, not being able to do return RedirectToAction("Index", new { id = scenario.ScenarioID }); after I save the scenario).

Does anybody have any suggestions for something else I could try to do?

4 Answers 4

0

I am assuming you want to avoid going an ajax route. If so, just put the message string in a cookie and test for the presence of a message in your view. If there is a message, display it. Since it is in a cookie you can retrieve the message even if you redirect after processing a successful post. Make sure to clear the cookie after you show the message!

We've formalized this process at my company and refer to it as the "Flash" (like Ruby on Rails used to, or maybe still does).

Ideally you could wrap the message setting, retrieval and display logic into a helper class to abstract its implementation from your view. That way you could use cookies, viewdata or even session values and your views and controllers would not have to change behavior.

2
  • I wouldn't mind going the Ajax route, but I run into some major problems when doing that. (As I said in my post, I can't do a RedirectToAction() which I need to do after submission.)
    – JasCav
    Jan 20, 2011 at 22:13
  • Then my approach will work for your case, no need to use ajax and you can still do your redirect. :) Jan 20, 2011 at 22:15
0

I think you should use jquery.post() to serve up a background post to prevent the page from reloading.

http://api.jquery.com/jQuery.post/

1
  • this is what Ajax.BeginForm does. Jan 20, 2011 at 21:38
0

Well, I just deny the submit and do it via AJAX:

$('#form').submit(function()
{
  return false;

  $.post('test.php', $(this).serialize(), function(data)
  {
    //Do your stuff. Data is the POST response data.
  });
});

Good luck!

2
  • That is what the Ajax.BeginForm is doing. Jan 20, 2011 at 21:39
  • What is it? I thought this was jQuery.
    – Blender
    Jan 20, 2011 at 21:59
0

Return the URL, and redirect on the client side. You could use a JsonResult or other to send data in MVC back to the client.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.