Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Show to end user that action was successful

Most of the ASP.NET MVC examples I have seen depict scenarios where a user is viewing an object (or collection of objects) and then moves from that page to one that displays a form that the user completes. Upon submitting the form with good input, the user is redirected back to the page that shows the object (or list) and the user can see that their changes were successful.

I've run into a scenario where there is no view or list page per business rules.

What are some good approaches for this scenario in ASP.NET MVC?

In the old days with Classic ASP and ASP.NET, I would process the input and then show the user a success message or the form with errors - all from the same page. This seems like it goes against best practices (SRP, no logic in views, etc.).

One easy approach is to redirect to a new page that tells the user their changes were successful, but then the user can visit that page any time. If I start putting in logic to protect against this (i.e. tempdata), the solution starts to feel dirty.

I could redirect to a landing page but there is no confirmation. Maybe I could rely on a messaging system that shows the end user a confirmation when they return to the landing page?

like image 423
Mayo Avatar asked Oct 05 '10 13:10

Mayo


2 Answers

I use a base controller that has a ShowMessage method. This method receives a message to be displayed (or a resource key, as you wish) and an enumeration that indicates the type of message (Success, Error, Notice, etc). Before returning in the action, I call ShowMessage with the appropriate message, e.g.

ShowMessage("A nice message here.", MessageType.Success);

Then in the master page I have a call to a helper method that renders a shared partial view which displays the messages in case there are any. Messages are stored in TempData with a fixed key. You can use the styles that appear in the Blueprint css framework for displaying the messages according to their type.

Here is the some code:

public void ShowMessage(string message, MessageType messageType)
{
    var messageViewModel = new MessageViewModel(message, messageType);
    TempData["Message"] = messageViewModel;
 }

The partial view:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyApp.Models.MessageViewModel>" %>
<%var className = Model.MessageType.ToString().ToLower(); %>
<div class="<%=className%>">
    <%=Html.Encode(Model.Message)%>
</div>

I´m sure there may be better approaches, like using filters, but this approach is very helpful for me.

like image 119
uvita Avatar answered Nov 11 '22 15:11

uvita


you can put something in viewdata and in the page do something like

<% if (viewdata["success"] != null){%>
<script>
$("#successDialog").dialog('open');
</script>
<%}%> 

and your user will see a popup telling him about success http://jqueryui.com/demos/dialog/

like image 33
Omu Avatar answered Nov 11 '22 15:11

Omu