asp.net, mvc3, PRG

Displaying Notification Message for POST-REDIRECT-GET in ASP.net MVC3

Logo

Introduction

There are several advantages of going ahead with GET-POST-REDIRECT way of web development style; however having the status information or notification from the page after the redirection is not that straight forward or not available out of the box in ASP.net MVC3.

PRG asp.net

Thank you Martijn Boland

I found the work by Martijn Boland on his blog post : Keep your users informed with ASP.NET MVC. Having a message or notification of the the action being performed after a GET-POST-REDIRECT cycle is something really awesome. Even better this works for ajax type request too. The formula for ajax implementation is that, he has leveraged the HTTP headers.

How do you do…?

Implemention

Create an Extension method for Controller

   1:  public static void ShowRedirectMessage(this Controller controller, string message)

   2:  {

   3:       controller.TempData["redirectMessage"] = message;

   4:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Create an Extension method for HtmlHelper

   1:  public static String RenderRedirectMessage(this HtmlHelper htmlHelper)

   2:  {     

   3:      string renderMessage = String.Empty;      

   4:      if (htmlHelper.ViewContext.TempData["redirectMessage"] != null)          

   5:          renderMessage = htmlHelper.ViewContext.TempData["redirectMessage"].ToString();      

   6:      return renderMessage;

   7:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Usage

Controller – POST

   1:  [AcceptVerbs(HttpVerbs.Post)]         

   2:  public ActionResult Upload(List files)         

   3:  {  

   4:            // Some action            

   5:            string status = “Transaction Successful”;           

   6:            this.ShowRedirectMessage(status);             

   7:            return RedirectToAction("Index");         

   8:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

View

   1:  <h3>@Html.RenderRedirectMessage()</h3>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }


Standard