I just started to learn MVC and am trying to understand how it works.
I don't want to send users to different views for all edit, insert and list operations.
In my sample application a View contains a list of items and below the list there is a form (for inserting new items) with action "{Controller}/Create" but there is no Create View.
When a user inserts a new item it posts to the Create action with httpverb post and creates the item and returns back to the List action with RedirectToAction method.
But I can not show any message(error, information etc) to the user in this style because I can not pass data between Create action and List action. How can I do that?
You need to use Post Redirect Get PRG pattern.
Please read this Use PRG Pattern for Data Modification section in this blog post by Kazi Manzur Rashid.
http://weblogs.asp.net/rashid/archive/2009/04/01/asp-net-mvc-best-practices-part-1.aspx
This approach uses
TempData
to maintainModelState
data between redirects.And here is your
Index
action method.And here is your
Index
view.ImportModelStateFromTempData
andExportModelStateToTempData
attributes helps transfer model state errors between redirects. This<% ViewContext.FormContext.ValidationSummaryId = "valSumCreateForm"; %>
associates the MVC Form with its corresponding Validation Summary.You can check another answer by me on this here as well. ViewModel with SelectList binding in ASP.NET MVC2
Let me know if you have any question.
-Soe
This article explains how to use TempData:
Most MVC frameworks have the ability to temporarily store a small bit of data just through the next request, for just this purpose. In ASP.NET MVC its called TempData, in Rails it's called :flash, etc.