詳解ASP.NET MVC PRG數(shù)據(jù)驗證
我們這里將要談到的是ASP.NET MVC PRG數(shù)據(jù)驗證,主要是參考一些國外關(guān)于PRG數(shù)據(jù)驗證的文章,希望對大家有所幫助。
我的理念:
既然是ASP.NET MVC,那就肯定要用PRG。但是簡單的PRG不能在輸入頁面顯示Html.ValidationMessage,另一個就是之前的數(shù)據(jù)會被全部清空或者初始化了。
想想要我是打了半天的字一下全沒了那多慘啊。你的訪客不氣傻了才怪。
OK,Google一下,找到了http://weblogs.asp.net/rashid/archive/2009/04/01/asp-net-mvc-best-practices-part-1.aspx
阿,他叫什么名字我不認(rèn)識,我也看不懂英文的版權(quán)聲明,所以這只有個鏈接沒署名了。誰認(rèn)識他叫他寫個C#版或者VB.NET版的版權(quán)聲明吧,謝謝。
英文不好不要緊,直接看第13點: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());
- }
自定義了兩個ActionFilter,阿,作者好像打錯別字了。您別在意。
- 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);
- }
- }
因為我用的是VB.NET,直接拿工具轉(zhuǎn)了放自己項目里,英文不懂照他的源代碼套上去一試。
哈,成功了,可愛的Html.ValidationMessage來了。
但是還是沒有解決數(shù)據(jù)被清空或初始化的問題。
這下慘了,Google也好,百度也好,還是在博客園里找這找那都沒找著。我估計可能我找的方法不對吧,不然這么多人都碰到的問題怎么沒人發(fā)出來呢。
最后在園子的小組里找到http://space.cnblogs.com/group/topic/7919/ 第三個回復(fù)有說ModelState.SetModelValue方法,拿過來一試,真不錯。
所以修改了一下剛才所說的兩個ActionFilter中的ExportModelStateToTempData;代碼如下:
- 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
最后說下,因為我用的VB.Net是不區(qū)分大小寫的,所以我的每個參數(shù)都用p開頭,臨時變量用t開頭,內(nèi)部循環(huán)用f開頭,客官將就著看吧。
原文標(biāo)題:Asp.Net Mvc PRG 數(shù)據(jù)驗證
鏈接:http://www.cnblogs.com/yexuan/archive/2009/09/18/1568929.html
【編輯推薦】