.NET ASP.NET MVC
Rails provides a way of passing messages between controller action requests via the flash facility.
The flash provides a way to pass temporary objects between actions. Anything you place in the flash will be exposed to the very next action and then cleared out. This is a great way of doing notices and alerts.
Why is this useful?
Post/Redirect/Get (PRG) is a common design pattern for web developers to help avoid certain duplicate form submissions. However it provides a small problem when trying to show the user a message, either success or failure, depending upon the what happened during the processing of the action during the POST request. As an example, when the user submits a form we might like to notify them that the request succeeded with a small message.
Cookies to the rescue
Just remember: Any flash message will be gone by the time the next request is submitted.
Styles for success, warning, and error messages or alerts
Following are examples of the four flash messages (warning, error, success and info) styled using the Bootstrap, from Twitter CSS toolkit.
Of course you are free to use your own CSS styling for the
alert-message container (and
info messages) the html is shown below:
Show me the code
jquery.cookie.js(standard jQuery cookie plugin) and
_Flash.cshtmlpartial within your main layout view. This will determine where the flash messages appear.
FlashMessageExtensions.csstatic extension method class to your website.
Within your MVC controllers simply use the
.Success("message to show")and the corresponding
Informationextension methods on
ActionResultto set the flash cookie on the response. Remember to add the necessary using statement to use these methods from flash extension class above.
By default the flash message will fade out after 3 seconds or if the user clicks on it. The fade-out delay is configurable by setting the