tags:

views:

371

answers:

2

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.

A: 

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.

Rob
Thanks, I've chosen your first suggestion and added a nullable boolean property to the Model to indicate the save result. In the Controller I set the value, and in the View I've added a code behind file so I have a property called "ResultMessage" that converts the boolean into a suitable message. :)
saille
A: 

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.

tvanfosson