日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

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

新聞中心

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

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

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

我的理念:

既然是ASP.NET MVC,那就肯定要用PRG。但是簡(jiǎn)單的PRG不能在輸入頁(yè)面顯示Html.ValidationMessage,另一個(gè)就是之前的數(shù)據(jù)會(huì)被全部清空或者初始化了。

想想要我是打了半天的字一下全沒了那多慘啊。你的訪客不氣傻了才怪。

OK,Google一下,找到了http://weblogs.asp.net/rashid/archive/2009/04/01/asp-net-mvc-best-practices-part-1.aspx

阿,他叫什么名字我不認(rèn)識(shí),我也看不懂英文的版權(quán)聲明,所以這只有個(gè)鏈接沒署名了。誰(shuí)認(rèn)識(shí)他叫他寫個(gè)C#版或者VB.NET版的版權(quán)聲明吧,謝謝。

英文不好不要緊,直接看第13點(diǎn):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());  

自定義了兩個(gè)ActionFilter,阿,作者好像打錯(cuò)別字了。您別在意。

 
 
 
  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.     }  

因?yàn)槲矣玫氖荲B.NET,直接拿工具轉(zhuǎn)了放自己項(xiàng)目里,英文不懂照他的源代碼套上去一試。

哈,成功了,可愛的Html.ValidationMessage來(lái)了。

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

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

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

所以修改了一下剛才所說(shuō)的兩個(gè)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 

最后說(shuō)下,因?yàn)槲矣玫腣B.Net是不區(qū)分大小寫的,所以我的每個(gè)參數(shù)都用p開頭,臨時(shí)變量用t開頭,內(nèi)部循環(huán)用f開頭,客官將就著看吧。


文章標(biāo)題:詳解ASP.NET MVC PRG數(shù)據(jù)驗(yàn)證
本文URL:http://www.dlmjj.cn/article/cooscoo.html