19

For error messages, validation faults etc you have

ModelState.AddErrorMessage("Fool!");

But, where do you put success responses like "You successfully transfered alot of money to your ex." + "Your balance is now zero". I still want to set it at the controller level and preferably in key-value way, the same way as errormessages but without invalidating the modelstate.

How is this usually done? ViewData?

Martin
  • 2,956
  • 7
  • 30
  • 59
  • 2
    I haven't looked into this option, but have you considered extending the ModelState object to include success messages as well as failure messages? – Odd Jan 27 '10 at 00:03

3 Answers3

24

I would populate TempData["success"] (or what ever key you want to give it) with the message I want to display within the controller, then redirect appropriately (e.g. if I edit a user, I redirect back to the user list). This relies on POST/Redirect/GET pattern - which is a good practice anyway.

TempData["success"] = "Your Balance is now zero";

In the master page I have a section that checks that variable and displays the message in a nice styled div. Something like (may not be 100% correct):

<% if(TempData["success"] != null) { %>
      <div id="SuccessMessage"><%= Html.Encode(TempData["success"]) %><div>
<% } %>
Code Pharaoh
  • 3,044
  • 2
  • 22
  • 26
Rosstified
  • 4,047
  • 2
  • 25
  • 33
  • 1
    if you dont want to use Post/Redirect/Get pattern, you can use ViewData rather than TempData to hold the value. I would very highly recommend using Post/Redirect/Get though... – Rosstified Jan 27 '10 at 00:53
  • That makes alot of sense. Where do you learn stuff like that? – Martin Jan 27 '10 at 09:47
  • In newer MVC you can do the same with something like [`ViewBag.Success`](http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications) – drzaus Mar 24 '14 at 14:46
  • Keep in mind that TempData uses Session and that there can be trouble on load balanced enviroments. –  Sep 29 '15 at 11:45
7

I suppose you could check the modelstate and set a variable in your model...

public ActionResult MyAction(MyEntity model)
{
  //Here would be some validation, which returns with ModelState errors

  //Now set the validity of the modelstate as the IsValid property in your entity
  model.IsValid = ModelState.IsValid;

  return View(model);
}

In your view...

<% if(Model.IsValid) { %>
  <p>You successfully transfered your balance to your ex.</p>
<% } %>

Edit: Given your updated question, I think you're looking at taking the wrong approach. I would go along with the other answers and follow a PRG pattern. This definitely makes more sense than trying to add a fake error.

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
  • Hey Dan, coming to my rescue again. This only gives me one possible response. I'll rephrase the question. – Martin Jan 26 '10 at 23:41
  • will successfull message displayed at the first visit of the page ? Because ModelState will valid too. – Muflix Dec 07 '16 at 15:51
  • 1
    @Muflix That's a good question. The code above is a for a `POST` request, so a `GET` request wouldn't hit this code. Also, the default value for a boolean is `false` so unless you explicitly set `IsValid` to `true` in the first request, this shouldn't be a problem. – Dan Atkinson Dec 08 '16 at 10:39
0

You should implement something like the POST/Redirect/GET pattern and "redirect" to another view at the end of your action methods after all validations were verified and everything executed fine. You can pass entire object instance to the destination view or you just pass plain text message, or you can pull out the text in the destination View itself from web.config or from Resource file.

For instance, I have one view in Shared folder named "ChangeSuccess.aspx" to which I redirect for all my successful edits&creates.

You "redirect" like this

return View("ChangeSuccess", objectInstance);

(note: doesn't actually redirect, see comments)

drzaus
  • 24,171
  • 16
  • 142
  • 201
mare
  • 13,033
  • 24
  • 102
  • 191
  • 1
    I am almost entirely sure that `return View` **does not perform a redirect**, but rather results in different content being returned by the same response. I think you meant `return this.Redirect("new-url")` or `return this.RedirectToAction(...)`. – drzaus Mar 24 '14 at 14:44
  • Although this is an old question, you are corrent in your comment. The above line does not perform redirection but rather renders a different view with specified model. – mare Mar 24 '14 at 15:31