详解ASP.NET MVC PRG数据验证
创始人
2024-06-07 11:41:17
0

我们这里将要谈到的是ASP.NET MVC PRG数据验证,主要是参考一些国外关于PRG数据验证的文章,希望对大家有所帮助。

我的理念:

既然是ASP.NET MVC,那就肯定要用PRG。但是简单的PRG不能在输入页面显示Html.ValidationMessage,另一个就是之前的数据会被全部清空或者初始化了。

想想要我是打了半天的字一下全没了那多惨啊。你的访客不气傻了才怪。

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

阿,他叫什么名字我不认识,我也看不懂英文的版权声明,所以这只有个链接没署名了。谁认识他叫他写个C#版或者VB.NET版的版权声明吧,谢谢。

英文不好不要紧,直接看第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,直接拿工具转了放自己项目里,英文不懂照他的源代码套上去一试。

哈,成功了,可爱的Html.ValidationMessage来了。

但是还是没有解决数据被清空或初始化的问题。

这下惨了,Google也好,百度也好,还是在博客园里找这找那都没找着。我估计可能我找的方法不对吧,不然这么多人都碰到的问题怎么没人发出来呢。

最后在园子的小组里找到http://space.cnblogs.com/group/topic/7919/ 第三个回复有说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是不区分大小写的,所以我的每个参数都用p开头,临时变量用t开头,内部循环用f开头,客官将就着看吧。

原文标题:Asp.Net Mvc PRG 数据验证

链接:http://www.cnblogs.com/yexuan/archive/2009/09/18/1568929.html

【编辑推荐】

  1. 详解ASP.NET MVC分页的实现方法
  2. ASP.NET MVC与WebForm区别谈
  3. ASP.NET MVC应用程序执行过程分析
  4. ASP.NET MVC分页控件的实现
  5. 有关ASP.NET MVC框架的一些基础知识

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...