3

I have a requirement that when a user successfully posts some form data, I would like a modal dialog to display whether the POST was successful or not, along with resetting the view to it's empty state (if successful).

How would I go about this?

I have the POST logic working correctly, but as it stands, there is no feedback indicating that the operation was a success or not.

user3907267
  • 238
  • 4
  • 8
  • possible duplicate of [What is the recommended approach to providing user notifications / confirmations in MVC?](http://stackoverflow.com/questions/3212730/what-is-the-recommended-approach-to-providing-user-notifications-confirmations) – CodeCaster Aug 08 '14 at 08:51
  • if need more help then plzz comment... – Kartikeya Khosla Aug 08 '14 at 09:21

2 Answers2

5

Answer 1:

  public ActionResult Index(string message)
    {
        if(!string.IsNullOrEmpty(message)){
        ViewData["successmessage"]=message;  //Or you can use Viewbag
        }
        return View();
    }

 [HttpPost]
 public ActionResult Index()
    {
        ...............
        return RedirectToAction("Index",new{ message="Saved successfully" });
    }

Just alert ViewData["successmessage"] on View with Javascript alert box.

On View just show alert box as alert('@ViewData["successmessage"]')

Answer 2:

 [HttpPost]
 public ActionResult Index()
    {
        ...............
        TempData["successmessage"] = "Saved successfully";
        return RedirectToAction("Index");
    }

View(Index.cshtml) :-

@{
var message = TempData["successmessage"] ?? string.Empty;
}

<script type="text/javascript">
var message = '@message';
if(message)
    alert(message);
</script>
Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69
  • There's no use to using TempData in a view if you set it in the same action method, in that case you'd better use ViewData/ViewBag or a model property. TempData is for use _in between_ two action methods, it uses the session. To use TempData properly, it will have to be set in your POST method and then read back in your GET method. See for example [Using Tempdata in ASP.NET MVC - Best practice](http://stackoverflow.com/questions/12422930/using-tempdata-in-asp-net-mvc-best-practice). – CodeCaster Aug 08 '14 at 08:55
  • Read my comment again. You set a query string parameter in POST, redirect to GET, then set TempData (uses session) which you then read _in that view_. That is not the proper use of TempData. – CodeCaster Aug 08 '14 at 08:59
  • Thanks. Just giving this a go now. – user3907267 Aug 08 '14 at 09:34
-2

Firstly, you have to add a listener at the front side, mostly with jquery.ajaxSetup function.

Then you need to let front listener know it is the successful request to display dialog, add a special code or something like that.

C# Code:

return Json(new{Code = 200, Message = "some text"}, JsonRequestBehavior.AllowGet);

Javascript Code:

$.ajaxSetup({
    success: function(xhr){
       var response = JSON.parse(xhr.responseText);
       if(response & response.Code == 200 && response.Message) {
           // pop up dialog with message here
       }
    }
})
Teddy
  • 787
  • 5
  • 13
  • How will this know that the previous POST was successful or not and what message to display? The question is not _"How to do an AJAX request"_, it's _"How to pass notifications from Post to Get in Post-Redirect-Get"_. – CodeCaster Aug 08 '14 at 09:05
  • The question is not about how to Poping up a modal dialog after a successful post request? – Teddy Aug 08 '14 at 09:11
  • Not according to the last sentence of the question. – CodeCaster Aug 08 '14 at 09:12
  • I think he want to add a plugin to pop up dialog automatically after a successful post without adding extra code in each request. – Teddy Aug 08 '14 at 09:15