saille
saille

Reputation: 9181

How to provide action "success" feedback to user in ASP.NET MVC?

Lets say we have an Edit View to edit our data, and we want to let the user know the result of their edit ie. to confirm that it was indeed saved successfully on the Model.

One solution is to assign a message to ViewData in the Edit Controller action method, and then use the View to display the message back to the user.

e.g. In the Edit Controller action method:

ViewData["EditResult"] = "All is well in the world.";

... and somewhere in the View:

<%= ViewData["EditResult"] %>

This is nice and easy, but is this the best way to provide feedback from the controller to the View? What are some other alternatives as I seem to be borderline on putting presentation type stuff in the Controller.

Upvotes: 5

Views: 2288

Answers (2)

tvanfosson
tvanfosson

Reputation: 532435

Typically I have a Show action that displays the state of the particular model. After a successful Update I will redirect to the Show action for that particular instance of the model and display the updated information. Note that there isn't any "success" message, but the changes are reflected in the updated model state. This is what I normally try to do: show the user the result of their action rather than a message indicating that the action was successful.

Upvotes: 0

Rob
Rob

Reputation: 48369

A very simple approach would be to pass some boolean or other status flag to the view as part of the model data; the view can then render that information as it sees fit.

Alternatively, you might want to consider having separate views for success vs. failure, since you may very well be rendering totally different content in each case.

Upvotes: 2

Related Questions