用于子属性的.Net MVC5自定义模型绑定器

时间:2015-11-06 20:57:23

标签: c# asp.net-mvc-5 handsontable

我有一个HandsontableModelBinder。该绑定器将绑定来自单个隐藏控件的手动json序列化数据。我以前绑定了一个隐藏控件列表,它工作但粘贴一个大的excel表时的性能是可怕的。现在性能很好,除了[Required]验证属性不会验证,因为这个复杂类型的内部属性不是真正的"绑定"。我通过反序列化隐藏的控件来绑定它们。

如何绑定/验证复杂类型的每个内部属性?

public class UnitedHandsontableListModelBinder : UnitedBaseModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var serializer = new JavaScriptSerializer();
        dynamic list = Activator.CreateInstance(bindingContext.ModelType) as IList;
        var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

        if (value != null && !string.IsNullOrWhiteSpace(value.AttemptedValue))
            list = serializer.Deserialize(value.AttemptedValue, bindingContext.ModelType);

        return ValidateModel(controllerContext, bindingContext, list);
    }
}

0 个答案:

没有答案