How to undo ASP.NET MVC action
It should be simple, but the answer eludes me. If I have a Save action in my controller and it fails, how can I undo the action without breaking the user input? For example, the index is strongly typed from "MyTable":
Function Index() As ActionResult
ViewData("message") = "Hello"
Return View(New MyTable)
End Function
<ActionName("Index"), AcceptVerbs(HttpVerbs.Post)> _
Function Save(ByVal form As MyTable) As ActionResult
Try
SaveMyData(form)
Return RedirectToAction("Index")
Catch
AddModelError("form", "An error occurred.")
???
End Try
End Function
In Catch, if I put Return View (form), I lose the message passed through the ViewData. If I redirect to the Index, I lose what the user entered. I think I've seen a simple (correct) way to deal with this before, but if you don't know what to look for, it's hard to find. What am I missing?
a source to share
In Catch, if I put a Return View (form), I lose the message passed through the ViewData.
In my opinion, you are not losing the message passed through the ViewData - this function just won't run.
And I would highly recommend doing a redirect or using TempData for what you are trying to achieve, there just is no point in doing it, not how MVC is supposed to work.
Going for what you have, I will have a private function that both actions call to return the view. It has your ViewData ("message") in one place and you still have the previous values in the form (and inside ModelState, like Josh E).
WARNING: VB.NET aerial code from C # programmer; -)
Function Index() As ActionResult
Return IndexView(New MyTable)
End Function
<ActionName("Index"), AcceptVerbs(HttpVerbs.Post)> _
Function Save(ByVal form As MyTable) As ActionResult
Try
SaveMyData(form)
Return RedirectToAction("Index")
Catch
AddModelError("form", "An error occurred.")
End Try
Return IndexView(form)
End Function
Private Function IndexView(ByVal form As MyTable) As ActionResult
ViewData("message") = "Hello"
Return View(form)
End Function
HTHS, Charles
Ps. I would like to add that it seemed strange to me that you have an index page that is posted back to itself, and then if there are errors in the message, it re-renders the index page, but if there are no errors, it is redirected back to the index page ...
I don't know what the app is doing, but it looks like you are using an index page / view for too many things.
a source to share
You can use TempData to store data from a form during redirection and retrieve data from TempData in an Index activity. Here's a discussion of TempData .
Your actions would then look like (excuse my background knowledge of VB.Net syntax):
Function Index() As ActionResult
ViewData("message") = "Hello"
If TempData["FormData"] == Nothing
Return View(New MyTable)
Else
Return View(TempData["FormData"])
EndIf
End Function
<ActionName("Index"), AcceptVerbs(HttpVerbs.Post)> _
Function Save(ByVal form As MyTable) As ActionResult
Try
SaveMyData(form)
Catch
AddModelError("form", "An error occurred.")
TempData["FormData"] = form;
End Try
Return RedirectToAction("Index")
End Function
TempData will only store data for one request from the same host. It's almost the same as the flash function in Rails.
a source to share