Asp.Net Mvc 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 

ContractedBlock.gif ExpandedBlockStart.gif Controller
[AcceptVerbs(HttpVerbs.Get), OutputCache(CacheProfile = "Dashboard"), StoryListFilter, ImportModelStateFromTempData]
public ActionResult Dashboard(string userName, StoryListTab tab, OrderBy orderBy, int? page)
{
    
//Other Codes
    return View();
}

[AcceptVerbs(HttpVerbs.Post), ExportModelStateToTempData]
public ActionResult Submit(string userName, string url)
{
    
if (ValidateSubmit(url))
    {
        
try
        {
            _storyService.Submit(userName, url);
        }
        
catch (Exception e)
        {
            ModelState.AddModelError(ModelStateException, e);
        }
    }

    
return Redirect(Url.Dashboard());
}

 

自定义了两个ActionFilter,阿,作者好像打错别字了。您别在意。

ContractedBlock.gif ExpandedBlockStart.gif ModelStateTempDataTransfer
public abstract class ModelStateTempDataTransfer : ActionFilterAttribute
{
    
protected static readonly string Key = typeof(ModelStateTempDataTransfer).FullName;
}

public class ExportModelStateToTempData : ModelStateTempDataTransfer
{
    
public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        
//Only export when ModelState is not valid
        if (!filterContext.Controller.ViewData.ModelState.IsValid)
        {
            
//Export if we are redirecting
            if ((filterContext.Result is RedirectResult) || (filterContext.Result is RedirectToRouteResult))
            {
                filterContext.Controller.TempData[Key] 
= filterContext.Controller.ViewData.ModelState;
            }
        }

        
base.OnActionExecuted(filterContext);
    }
}

public class ImportModelStateFromTempData : ModelStateTempDataTransfer
{
    
public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        ModelStateDictionary modelState 
= filterContext.Controller.TempData[Key] as ModelStateDictionary;

        
if (modelState != null)
        {
            
//Only Import if we are viewing
            if (filterContext.Result is ViewResult)
            {
                filterContext.Controller.ViewData.ModelState.Merge(modelState);
            }
            
else
            {
                
//Otherwise remove it.
                filterContext.Controller.TempData.Remove(Key);
            }
        }

        
base.OnActionExecuted(filterContext);
    }
}

 

因为我用的是VB.Net,直接拿工具转了放自己项目里,英文不懂照他的源代码套上去一试。

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

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

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

最后在园子的小组里找到http://space.cnblogs.com/group/topic/7919/ 第三个回复有说ModelState.SetModelValue方法,拿过来一试,真不错。

所以修改了一下刚才所说的两个ActionFilter中的ExportModelStateToTempData;代码如下:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
    Public Overrides Sub OnActionExecuted(ByVal pFilterContext As ActionExecutedContext)
        
If Not pFilterContext.Controller.ViewData.ModelState.IsValid Then
            
If TypeOf (pFilterContext.Result) Is RedirectResult OrElse TypeOf (pFilterContext.Result) Is RedirectToRouteResult Then
                
If pFilterContext.HttpContext.Request.Form.Count > 0 Then
                    
Dim tFormCollection As New FormCollection(pFilterContext.HttpContext.Request.Form)
                    
For Each fKey As String In tFormCollection
                        pFilterContext.Controller.ViewData.ModelState.SetModelValue(fKey, tFormCollection.ToValueProvider(fKey))
                    
Next
                    pFilterContext.Controller.TempData(mKey) 
= pFilterContext.Controller.ViewData.ModelState
                
End If
            
End If
        
End If
        
MyBase.OnActionExecuted(pFilterContext)
    
End Sub

 

阿,最后说下,因为我用的VB.Net是不区分大小写的,所以我的每个参数都用p开头,临时变量用t开头,内部循环用f开头,客官将就着看吧。

转载于:https://www.cnblogs.com/yexuan/archive/2009/09/18/1568929.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值