日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
詳解ASP.NET MVC PRG數(shù)據(jù)驗證

我們這里將要談到的是ASP.NET MVC PRG數(shù)據(jù)驗證,主要是參考一些國外關(guān)于PRG數(shù)據(jù)驗證的文章,希望對大家有所幫助。

為麻栗坡等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及麻栗坡網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都做網(wǎng)站、網(wǎng)站建設(shè)、麻栗坡網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!

我的理念:

既然是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開頭,客官將就著看吧。


本文題目:詳解ASP.NET MVC PRG數(shù)據(jù)驗證
地址分享:http://m.5511xx.com/article/cooscoo.html