我试图做到以下几点。

使用默认模型粘合剂结合从查询字符串值的对象。结果 如果失败,我然后尝试和对象从cookie值结合。

但是我使用该对象上dataannotations和我有以下的问题。

  1. 如果没有查询字符串参数的默认模型绑定甚至不登记所需字段的任何验证错误。这显然不火,即使这些验证,如果房产本身的查询字符串集合中是没有的。我怎样才能改变这种行为?我想必填字段是错误的,如果他们不是在查询字符串。
  2. 如果我有模型验证错误,我想然后加载从cookie中的模型,然后重新验证对象。我不知道如何让模型绑定验证我已填充自己的对象。
  3. 下面是我到目前为止所。

        public class MyCarBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var myCar = base.BindModel(controllerContext, bindingContext);
    
            if (!bindingContext.ModelState.IsValid)
            {
                myCar = MyCar.LoadFromCookie();
                // Not sure what to do to revalidate
            }
    
            return myCar;
        }
    }
    

    如何正确地做到这一点任何帮助,将不胜感激。

有帮助吗?

解决方案

好了,我解决了它自己。万一有人在这里张贴的解决方案有意见或可能喜欢使用它。

 public class MyCarBinder : DefaultModelBinder
 {
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var queryStringBindingContext = new ModelBindingContext()
        {
            FallbackToEmptyPrefix = bindingContext.FallbackToEmptyPrefix,
            ModelMetadata = bindingContext.ModelMetadata,
            ModelName = bindingContext.ModelName,
            PropertyFilter = bindingContext.PropertyFilter,
            ValueProvider = new QueryStringValueProvider(controllerContext),
            ModelState = new ModelStateDictionary()
        };

        var myCar = base.BindModel(controllerContext, queryStringBindingContext);

        if (queryStringBindingContext.ModelState.IsValid)
            return myCar;

        // try to bind from cookie if query string is invalid
        var cookieHelper = new Helpers.ControllerContextCookieHelper(controllerContext);
        NameValueCollection nvc = cookieHelper.GetCookies(Helpers.CookieName.MyCar);

        if (nvc == null)
        {
            bindingContext.ModelState.Merge(queryStringBindingContext.ModelState);
            return myCar;
        }

        var cookieBindingContext = new ModelBindingContext()
        {
            FallbackToEmptyPrefix = bindingContext.FallbackToEmptyPrefix,
            ModelMetadata = bindingContext.ModelMetadata,
            ModelName = bindingContext.ModelName,
            PropertyFilter = bindingContext.PropertyFilter,
            ValueProvider = new NameValueCollectionValueProvider(nvc, CultureInfo.InvariantCulture),
            ModelState = new ModelStateDictionary()
        };

        var myCarFromCookie = base.BindModel(controllerContext, cookieBindingContext);

        if (cookieBindingContext.ModelState.IsValid)
        {
            MyCar temp = myCarFromCookie as MyCar;
            if (temp != null)
                temp.FromCookie = true;

            return myCarFromCookie;
        }
        else
        {
            bindingContext.ModelState.Merge(queryStringBindingContext.ModelState);
            return myCar;
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top