1

Imagine a page where the user can update, delete, edit or even vote the input. Since each action will be handled by a different ActionResult, the url of the page will have to change in case there are errors. My question is simple: Say your page is "http://localhost/Input/" and when you there are some errors, I want to redirect to "http://localhost/Input/Error" and display an error message. I don't want to use sessions for this therefore please don't show me this link. Basically, want I want to do this is something similar:

public ActionResult Create(FormCollection form) { 
        try {
            // some code here
            return RedirectToAction("Index");
        }
        catch () {
             string errorMessage = "You have done something bad!"; 
             // and I want to pass this string to my Error Action.
             return RedirectToAction("Error"); 
        }
}

The Solution I've Used

I know that I said I don't want to use TempData, but apparantly that is the best option and I've used that for my issue. As for the answer I've chosen DigBySwift's answer because that's the most logical thing you can do if you don't want to use Session for this type of operation.

Community
  • 1
  • 1
Shaokan
  • 7,438
  • 15
  • 56
  • 80

3 Answers3

4

As you say, if you don't want to use sessions then TempData is not an option. Ideally, you should try not to pass the error message to the next page. Instead store the error message(s) in a resource file.

You could pass an id/enum to you next page and then retrieve the error based on the passed parameter:

return RedirectToAction("Error", new { ErrorId = 4 });

public ActionResult Error(int errorId){ ... }

Edit: An alternative (since a db write-read would be expensive considering) would be to write the message to a cookie and then retrieve it after the redirect, and then delete the cookie. This is not great and I personally would not use this. But if you need to customise the message, it is an option.

Digbyswift
  • 10,310
  • 4
  • 38
  • 66
1

Is there an option of saving the error message in database from the sender page and getting it back on the other page ? I know its similar to the one posted above but it will be more dynamic as you dont need to put the message in resource file and you can save the error message in db and get that on other page

Ankit
  • 6,388
  • 8
  • 54
  • 79
  • That's always an option :) But I believe as Digbyswift suggested, it is better to store an id within an enum and use like that :) – Shaokan Jul 17 '11 at 18:45
  • Yeah that is a good option unless you want to show a custom message. – Ankit Jul 17 '11 at 18:48
  • 1
    For a custom message, you could write a cookie and retrieve it on the redirect. I have edited my answer to describe this. A DB write and then read is a little expensive for this operation in my opinion. – Digbyswift Jul 17 '11 at 19:09
0

I'd like to suggest an alternative way. I use AJAX to do these types of operations, thus instead of redirecting user to another view, I can return a view, or mostly a partial view from my controller. Thus I can use that string message right inside my view using ViewBag.

public ActionResult Create(FormCollection form) { 
    try {
        // some code here
        return View('CreationSuccess');
    }
    catch () {
         ViewBag.errorMessage = "You have done something bad!"; 
         // and I want to pass this string to my Error Action.
         return View('CrudError');
    }
}
Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188
  • Unfortunately, I do not use AJAX yet. I am a little obsessed with this issue, I'd like my page to work either with and without js. Therefore, once I complete running the whole site without js then I will add AJAX feature – Shaokan Jul 17 '11 at 18:43
  • IMO, the problem with this approach is that user may just refresh the page and cause the code to re-run and re-error. This is not good practice especially if the operation is expensive. Also Viewbag is MVC3 only and therefore .NET 4 only. – Digbyswift Jul 17 '11 at 18:48