Displaying success dialog in HTTP message

I have a requirement that when the user has successfully posted some form data, I would like the modal dialog to display whether the POST was successful or not, and also reset the view to an empty state (if it was successful).

How can i do this?

I have the POST logic working correctly, but as it is, there is no feedback to indicate whether the operation was successful or not.

+3


source to share


2 answers


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 report ViewData["successmessage"]

in the View box with a Javascript alert box.

In view mode, just show the 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>

      

+4


source


First, you have to add a front-side listener, basically using the jquery.ajaxSetup function.

Then you need to tell the front listener that it is a successful request to display the dialog, add custom code, or something.

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
       }
    }
})

      

-2


source







All Articles