自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解ASP.NET MVC PRG數(shù)據(jù)驗證

開發(fā) 后端
我們這里將介紹ASP.NET MVC PRG數(shù)據(jù)驗證,還可以用在VB.NET下。希望本文能對大家有所幫助。

我們這里將要談到的是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 

  1. Controller  
  2. [AcceptVerbs(HttpVerbs.Get), OutputCache(CacheProfile = "Dashboard"), StoryListFilter, ImportModelStateFromTempData]  
  3. public ActionResult Dashboard(string userName, StoryListTab tab, OrderBy orderBy, int? page)  
  4. {  
  5.     //Other Codes  
  6.     return View();  
  7. }  
  8.  
  9. [AcceptVerbs(HttpVerbs.Post), ExportModelStateToTempData]  
  10. public ActionResult Submit(string userName, string url)  
  11. {  
  12.     if (ValidateSubmit(url))  
  13.     {  
  14.         try 
  15.         {  
  16.             _storyService.Submit(userName, url);  
  17.         }  
  18.         catch (Exception e)  
  19.         {  
  20.             ModelState.AddModelError(ModelStateException, e);  
  21.         }  
  22.     }  
  23.  
  24.     return Redirect(Url.Dashboard());  

自定義了兩個ActionFilter,阿,作者好像打錯別字了。您別在意。

  1. ModelStateTempDataTransfer  
  2. public abstract class ModelStateTempDataTransfer : ActionFilterAttribute  
  3. {  
  4.     protected static readonly string Key = typeof(ModelStateTempDataTransfer).FullName;  
  5. }  
  6.  
  7. public class ExportModelStateToTempData : ModelStateTempDataTransfer  
  8. {  
  9.     public override void OnActionExecuted(ActionExecutedContext filterContext)  
  10.     {  
  11.         //Only export when ModelState is not valid  
  12.         if (!filterContext.Controller.ViewData.ModelState.IsValid)  
  13.         {  
  14.             //Export if we are redirecting  
  15.             if ((filterContext.Result is RedirectResult) || (filterContext.Result is RedirectToRouteResult))  
  16.             {  
  17.                 filterContext.Controller.TempData[Key] = filterContext.Controller.ViewData.ModelState;  
  18.             }  
  19.         }  
  20.  
  21.         base.OnActionExecuted(filterContext);  
  22.     }  
  23. }  
  24.  
  25. public class ImportModelStateFromTempData : ModelStateTempDataTransfer  
  26. {  
  27.     public override void OnActionExecuted(ActionExecutedContext filterContext)  
  28.     {  
  29.         ModelStateDictionary modelState = filterContext.Controller.TempData[Key] as ModelStateDictionary;  
  30.  
  31.         if (modelState != null)  
  32.         {  
  33.             //Only Import if we are viewing  
  34.             if (filterContext.Result is ViewResult)  
  35.             {  
  36.                 filterContext.Controller.ViewData.ModelState.Merge(modelState);  
  37.             }  
  38.             else  
  39.             {  
  40.                 //Otherwise remove it.  
  41.                 filterContext.Controller.TempData.Remove(Key);  
  42.             }  
  43.         }  
  44.  
  45.         base.OnActionExecuted(filterContext);  
  46.     }  

因為我用的是VB.NET,直接拿工具轉(zhuǎn)了放自己項目里,英文不懂照他的源代碼套上去一試。

哈,成功了,可愛的Html.ValidationMessage來了。

但是還是沒有解決數(shù)據(jù)被清空或初始化的問題。

這下慘了,Google也好,百度也好,還是在博客園里找這找那都沒找著。我估計可能我找的方法不對吧,不然這么多人都碰到的問題怎么沒人發(fā)出來呢。

最后在園子的小組里找到http://space.cnblogs.com/group/topic/7919/ 第三個回復(fù)有說ModelState.SetModelValue方法,拿過來一試,真不錯。

所以修改了一下剛才所說的兩個ActionFilter中的ExportModelStateToTempData;代碼如下:

  1. Code  
  2.     Public Overrides Sub OnActionExecuted(ByVal pFilterContext As ActionExecutedContext)  
  3.         If Not pFilterContext.Controller.ViewData.ModelState.IsValid Then  
  4.             If TypeOf (pFilterContext.Result) Is RedirectResult OrElse TypeOf (pFilterContext.Result) Is RedirectToRouteResult Then  
  5.                 If pFilterContext.HttpContext.Request.Form.Count > 0 Then  
  6.                     Dim tFormCollection As New FormCollection(pFilterContext.HttpContext.Request.Form)  
  7.                     For Each fKey As String In tFormCollection  
  8.                         pFilterContext.Controller.ViewData.ModelState.SetModelValue(fKey, tFormCollection.ToValueProvider(fKey))  
  9.                     Next  
  10.                     pFilterContext.Controller.TempData(mKey) = pFilterContext.Controller.ViewData.ModelState  
  11.                 End If  
  12.             End If  
  13.         End If  
  14.         MyBase.OnActionExecuted(pFilterContext)  
  15.     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

【編輯推薦】

  1. 詳解ASP.NET MVC分頁的實現(xiàn)方法
  2. ASP.NET MVC與WebForm區(qū)別談
  3. ASP.NET MVC應(yīng)用程序執(zhí)行過程分析
  4. ASP.NET MVC分頁控件的實現(xiàn)
  5. 有關(guān)ASP.NET MVC框架的一些基礎(chǔ)知識
責(zé)任編輯:彭凡 來源: 博客園
相關(guān)推薦

2010-03-19 09:17:16

ASP.NET MVC

2009-11-24 15:11:21

ASP.NET MVC

2009-09-11 09:18:17

ASP.NET MVC

2009-08-04 16:50:26

2009-07-31 12:43:59

ASP.NET MVC

2009-07-24 13:20:44

MVC框架ASP.NET

2009-07-29 12:55:44

ASP.NET身份驗證

2009-10-29 09:15:32

ASP.NET MVCDropDownLis

2010-10-12 09:52:02

ASP.NET MVC

2009-03-31 13:12:05

ASP.NETMVC表單驗證

2011-04-12 13:53:25

ASP.NET MVCjQuery

2009-09-10 09:50:47

ASP.NET MVC

2009-07-29 09:59:10

ASP.NET For

2009-07-23 15:44:39

ASP.NET MVC

2009-07-22 10:09:59

ASP.NET MVC

2009-07-23 14:31:20

ASP.NET MVC

2009-07-22 13:24:24

ASP.NET MVC

2009-07-20 10:53:59

ASP.NET MVC

2010-01-26 13:15:42

ASP.NET MVC

2010-02-03 09:50:58

ASP.NET MVC
點贊
收藏

51CTO技術(shù)棧公眾號