0

我已经在我的文件上传操作中实现了自定义模型绑定器。有时文件上传会被服务器丢弃,并且使用部分数据调用 BindModel 方法(ContentLenght 和 TotalBytes 在这里不匹配)。我想从自定义模型绑定器中止动作执行,该怎么做?

 public class OptionModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var optionModelName = GetOptionModelName(controllerContext);
            if (optionModelName != null) return null// !!!How to abort Action execution?!!! here

                Trace.TraceInformation(optionModelName);
                var model = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(optionModelName);
                bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, model.GetType());

            return base.BindModel(controllerContext, bindingContext);
        }

public class OptionModelBinderAttribute : CustomModelBinderAttribute
{

    public override IModelBinder GetBinder()
    {
        return new OptionModelBinder();
    }
}

[HttpPost]
public ActionResult UploadFile(IEnumerable<HttpPostedFileBase> clientUpload, [OptionModelBinder]IOptionViewModel formData)
{
}
4

1 回答 1

0

这不是您想从模型绑定中做的事情。
模型绑定不应控制逻辑行为。它没有任何意义。
我建议在控制器中,您会询问某些内容是否为空并将适当的结果返回给客户端。
让模型绑定做控制器的工作是不对的。

于 2013-10-30T05:54:31.877 回答