Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC: How to display success confirmation message after server-side processing

I have a requirement to display a message confirming a successful database update in an ASP.NET MVC application. Currently the application only shows messages (using a ValidationSummary helper) when an error occurs. On a successful operation, the application currently redirects to a suitable point in the navigation.

Goals are:

  • Display the confirmation message in an appropriate way
  • Minimise user actions required to proceed after reading message
  • Avoid an extra post / round-trip to display the message
  • Minimise development effort and risk inserting a message at multiple points in the application

My preference would be some sort of tool-tip type message display near the submit button and then a mechanism for removing the message and proceeding with the existing redirection after success.

That seems to suggest an Ajax call rather than the existing HTTP POST to submit the form. How would I go about this?

like image 662
Paul Taylor Avatar asked Nov 24 '12 12:11

Paul Taylor


Video Answer


2 Answers

I Would use TempData["key"]

This is like ViewData["key"] however the data persists for the next HttpRequest and is disposed automatically by asp.net after this

So you can do this.

Controller Action

[HttpPost] public ActionResult SomePostAction(SomeViewModel vm) {    if(ModelState.IsValid) // Is User Input Valid?    {        try        {            CommitData();            TempData["UserMessage"] = new MessageVM() { CssClassName = "alert-sucess", Title = "Success!", Message = "Operation Done." };            return RedirectToAction("Success");        }        catch(Exception e)        {            TempData["UserMessage"] =  new MessageVM() { CssClassName = "alert-error", Title = "Error!", Message = "Operation Failed." };            return RedirectToAction("Error");        }     }     return View(vm); // Return View Model with model state errors } 

_Layout.cshtml

<!DOCTYPE html>    <html>      <head>       </head>      <body>       @if(TempData["UserMessage"] != null)       {            var message = (MessageVM)TempData["UserMessage"];           <div class="alert @message.CssClassName">                <strong>@message.Title</strong>                 @message.Message           </div>       }           @RenderBody()      </body> </html> 

More Info: http://www.devcurry.com/2012/05/what-is-aspnet-mvc-tempdata.html

like image 80
SimonGates Avatar answered Sep 18 '22 14:09

SimonGates


On a successful operation ,you just store the success message description into ViewBag like as

ViewBag.successMessage="Success"  

then in view check the ViewBag value is null or not? through javascript ,if not null show the message in Div

if('@ViewBag.successMessage'!="") {    $('#divSuccessMessage').show(); }  else {   $('#divSuccessMessage').hide(); } 

default in page load hide the div

like image 23
Anand Avatar answered Sep 22 '22 14:09

Anand