views:

55

answers:

2

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?

+1  A: 

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/

Omu
If you user Post-Redirect-Get (PRG) pattern, you will have to use TempData instead of ViewData.
uvita
@uvita not necessarily, it could be something like return RedirectToAction("Index", new{success=true}); and the index action sets the viewdata
Omu
+2  A: 

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.

uvita
I have to point out, that if user aborts request and/or quickly clicks on another link (navigates to another page), then user will see your message from TempData in an incorrect and confusing environment. This is because TempData will be picked up by any first user's request that hits the server.
queen3