欢迎来到.net学习网

欢迎联系站长一起更新本网站!QQ:879621940

您当前所在位置:首页 » ASP.Net » 正文

热门阅读

详解ASP.NET MVC PRG数据验证

创建时间:2011年06月16日 22:54  阅读次数:(4598)
分享到:

我们这里将要谈到的是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.             {  
    来源:
    说明:所有来源为 .net学习网的文章均为原创,如有转载,请在转载处标注本页地址,谢谢!
    【编辑:Wyf

    打赏

    取消

    感谢您的支持,我会做的更好!

    扫码支持
    扫码打赏,您说多少就多少

    打开支付宝扫一扫,即可进行扫码打赏哦

最新评论

共有评论0条
  • 暂无任何评论,请留下您对本文章的看法,共同参入讨论!
发表评论:
留言人:
内  容:
请输入问题 59+78=? 的结果(结果是:137)
结  果: