c# - 具有多态对象集合的复杂模型的自定义模型绑定(bind)器

标签 c# asp.net-mvc custom-model-binder

如何为包含多态对象集合的复杂模型编写自定义模型绑定(bind)程序?

我有下一个模型结构:

public class CustomAttributeValueViewModel
{
    public int? CustomAttributeValueId { get; set; }
    public int CustomAttributeId { get; set; }
    public int EntityId { get; set; }
    public CustomisableTypes EntityType { get; set; }
    public string AttributeClassType { get; set; }
}

public class CustomStringViewModel : CustomAttributeValueViewModel
{
    public string Value { get; set; }
}

public class CustomIntegerViewModel : CustomAttributeValueViewModel
{
    public int Value { get; set; }
}

如果我想将 CustomAttributeValueViewModel 绑定(bind)到它的某些继承者,我会使用这样的自定义模型绑定(bind)器:

public class CustomAttributeValueModelBinder : DefaultModelBinder
{
    protected override object CreateModel(
        ControllerContext controllerContext,
        ModelBindingContext bindingContext,
        Type modelType)
    {
        if (modelType == typeof(CustomAttributeValueViewModel))
        {
            var attributeClassType = (string)bindingContext.ValueProvider
                .GetValue("AttributeClassType")
                .ConvertTo(typeof(string));

            Assembly assembly = typeof(CustomAttributeValueViewModel).Assembly;
            Type instantiationType = assembly.GetType(attributeClassType, true);

            var obj = Activator.CreateInstance(instantiationType);
            bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, instantiationType);
            bindingContext.ModelMetadata.Model = obj;
            return obj;
        }

        return base.CreateModel(controllerContext, bindingContext, modelType);
    }
}

效果很好。但现在我想将此类模型绑定(bind)为另一个模型的集合项。例如:

public class SomeEntity
{
    // different properties here

    public IList<CustomAttributeValueViewModel> CustomAttributes { get; set; }
}

我该怎么做?

已编辑:

我想绑定(bind)从客户那里收到的发布数据。为了更清楚起见,这是我的 POST HTTP 请求的示例:

POST someUrl HTTP/1.1
User-Agent: Fiddler
Host: localhost
Content-Type: application/json; charset=utf-8
Content-Length: 115

{
  "ProductName": "Product Name",
  "CustomAttributeValues": [
    {
      "CustomAttributeId": "1",
      "Value": "123",
      "AttributeClassType": "namespace.CustomStringViewModel"
    }
  ]
}

我在操作中收到了这些数据:

public void Save([ModelBinder(typeof(SomeBinder))] SomeEntity model)
{
    // some logic
}

我想写这样的 Binder 来获取继承者的集合。

最佳答案

您需要包含 AttributeClassType 的完整路径,

var valueProviderResult = bindingContext.ValueProvider
                            .GetValue(bindingContext.ModelName + ".AttributeClassType");

请看这个working Github sample

关于c# - 具有多态对象集合的复杂模型的自定义模型绑定(bind)器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26709747/

相关文章:

asp.net-mvc - 如何从自定义 ModelBinder 中调用 UpdateModel? (MVC)

c# - 具有规范布局的搜索树

c# - 如何告诉 unit ms test 从子文件夹而不是 root 中获取 CSV 文件?

javascript - Handlebars 和文本区域

asp.net-mvc - Spark 中嵌套了 "Layouts"?

asp.net-mvc - 将依赖项注入(inject)自定义模型绑定(bind)器并使用 InRequestScope 使用 Ninject

c# - 方法签名与文本框事件处理程序不匹配

c# - 如何使用knockout js指定name属性

javascript - 暂停动画 2 秒,然后在 jQuery 中自动恢复它(不是在鼠标悬停或鼠标移出时)?

c# - 属性的自定义模型 Binder