Here we will talk about ASP. net mvc prg data verification, mainly for reference to some foreign articles on PRG data verification, I hope to help you.
My philosophy:
Since it is ASP. net mvc, PRG must be used. However, a simple PRG cannot display Html. ValidationMessage on the input page. The other is that the previous data is cleared or initialized.
Think about how miserable it is if I have been playing for half a day. It's strange that your visitor is not so angry.
Okay, Google, find the http://weblogs.asp.net/rashid/archive/2009/04/01/asp-net-mvc-best-practices-part-1.aspx
A: I don't know his name. I don't know the copyright statement in English, so there is no signature for this link. Who knows him and asks him to write a copyright statement for C # Or VB. NET? Thank you.
It doesn't matter if the English is not good. Let's look at the 13th point: Use PRG Pattern for Data Modification.
- Controller
- [AcceptVerbs(HttpVerbs.Get), OutputCache(CacheProfile = "Dashboard"), StoryListFilter, ImportModelStateFromTempData]
- public ActionResult Dashboard(string userName, StoryListTab tab, OrderBy orderBy, int? page)
- {
- //Other Codes
- return View();
- }
-
- [AcceptVerbs(HttpVerbs.Post), ExportModelStateToTempData]
- public ActionResult Submit(string userName, string url)
- {
- if (ValidateSubmit(url))
- {
- try
- {
- _storyService.Submit(userName, url);
- }
- catch (Exception e)
- {
- ModelState.AddModelError(ModelStateException, e);
- }
- }
-
- return Redirect(Url.Dashboard());
- }
Two custom actionfilters are defined. A, the author seems to have typed a wrong word. Don't worry.
- ModelStateTempDataTransfer
- public abstract class ModelStateTempDataTransfer : ActionFilterAttribute
- {
- protected static readonly string Key = typeof(ModelStateTempDataTransfer).FullName;
- }
-
- public class ExportModelStateToTempData : ModelStateTempDataTransfer
- {
- public override void OnActionExecuted(ActionExecutedContext filterContext)
- {
- //Only export when ModelState is not valid
- if (!filterContext.Controller.ViewData.ModelState.IsValid)
- {
- //Export if we are redirecting
- if ((filterContext.Result is RedirectResult) || (filterContext.Result is RedirectToRouteResult))
- {
- filterContext.Controller.TempData[Key] = filterContext.Controller.ViewData.ModelState;
- }
- }
-
- base.OnActionExecuted(filterContext);
- }
- }
-
- public class ImportModelStateFromTempData : ModelStateTempDataTransfer
- {
- public override void OnActionExecuted(ActionExecutedContext filterContext)
- {
- ModelStateDictionary modelState = filterContext.Controller.TempData[Key] as ModelStateDictionary;
-
- if (modelState != null)
- {
- //Only Import if we are viewing
- if (filterContext.Result is ViewResult)
- {
- filterContext.Controller.ViewData.ModelState.Merge(modelState);
- }
- else
- {
- //Otherwise remove it.
- filterContext.Controller.TempData.Remove(Key);
- }
- }
-
- base.OnActionExecuted(filterContext);
- }
- }
I used VB. NET and directly transferred it to my project using tools. I didn't know how to use the source code for a try.
Haha, it succeeded. The cute Html. ValidationMessage is coming.
However, data is still not cleared or initialized.
This is terrible. Google, Baidu, or I am still looking for it in my blog. I guess the method I am looking for is wrong. Otherwise, no one will issue any problems that many people have encountered.
Finally in the garden of the group to find the http://space.cnblogs.com/group/topic/7919/ third reply said ModelState. SetModelValue method, take a try, really good.
So I modified the ExportModelStateToTempData in the two actionfilters mentioned earlier. The Code is as follows:
- Code
- Public Overrides Sub OnActionExecuted(ByVal pFilterContext As ActionExecutedContext)
- If Not pFilterContext.Controller.ViewData.ModelState.IsValid Then
- If TypeOf (pFilterContext.Result) Is RedirectResult OrElse TypeOf (pFilterContext.Result) Is RedirectToRouteResult Then
- If pFilterContext.HttpContext.Request.Form.Count > 0 Then
- Dim tFormCollection As New FormCollection(pFilterContext.HttpContext.Request.Form)
- For Each fKey As String In tFormCollection
- pFilterContext.Controller.ViewData.ModelState.SetModelValue(fKey, tFormCollection.ToValueProvider(fKey))
- Next
- pFilterContext.Controller.TempData(mKey) = pFilterContext.Controller.ViewData.ModelState
- End If
- End If
- End If
- MyBase.OnActionExecuted(pFilterContext)
- End Sub
In the end, because I use VB. net is case-insensitive, so each of my parameters starts with p, temporary variables start with t, and internal loops start with f.
Original article title: Asp. Net Mvc PRG data verification
Link: http://www.cnblogs.com/yexuan/archive/2009/09/18/1568929.html
- Detailed description of ASP. net mvc paging Implementation Method
- Differences between ASP. net mvc and WebForm
- ASP. net mvc application Execution Process Analysis
- Implementation of ASP. net mvc paging controls
- Some basic knowledge about ASP. net mvc Framework