Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC将子类型集合绑定到post模型_C#_Asp.net Mvc_Inheritance_Model_Model Binding - Fatal编程技术网

C# MVC将子类型集合绑定到post模型

C# MVC将子类型集合绑定到post模型,c#,asp.net-mvc,inheritance,model,model-binding,C#,Asp.net Mvc,Inheritance,Model,Model Binding,我的问题是: 要获取问题的当前子类型,我不能只调用GetValue(“ModelType”),因为问题是一个集合。那么,如何在自定义模型绑定器中获取集合中当前问题的子类型呢?所以这是行不通的: var typeValue = bindingContext.ValueProvider.GetValue("ModelType"); 下面是我的代码的简化版本。。我有以下型号: 公共类PostViewModel { 公共字符串标题{get;set;} 公共列表问题{get;set;} } 公开课问题

我的问题是: 要获取问题的当前子类型,我不能只调用GetValue(“ModelType”),因为问题是一个集合。那么,如何在自定义模型绑定器中获取集合中当前问题的子类型呢?所以这是行不通的:

var typeValue = bindingContext.ValueProvider.GetValue("ModelType");
下面是我的代码的简化版本。。我有以下型号:

公共类PostViewModel
{
公共字符串标题{get;set;}
公共列表问题{get;set;}
}
公开课问题
{
公共int Id{get;set;}
}
公共课单选:问题
{
公共列表应答选项{get;set;}
}
公共类文本问题:问题
{
公共字符串值{get;set;}
}
然后,我创建了以下模型绑定器:

公共类问题ModelBinder:DefaultModelBinder
{
受保护的重写对象CreateModel(ControllerContext ControllerContext,ModelBindingContext bindingContext,Type modelType)
{
if(modelType.Equals(typeof(Question)))
{
var typeValue=bindingContext.ValueProvider.GetValue(“模型类型”);
var type=type.GetType(
(string)typeValue.ConvertTo(typeof(string)),
真的
);
如果(!typeof(Question).IsAssignableFrom(type))
{
抛出新的InvalidOperationException(“坏类型”);
}
var obj=Activator.CreateInstance(类型);
bindingContext.ModelMetadata=ModelMetadataProviders.Current.GetMetadataForType(()=>obj,type);
bindingContext.ModelMetadata.Model=obj;
返回obj;
}
返回base.CreateModel(controllerContext、bindingContext、modelType);
}
}
我的观点是这样的:

//查看A

//视图B

@model QuestionViewModel
@Html.HiddenFor(m=>m.Id)
@隐藏(“ModelType”,Model.GetType())
@开关(型号.类型)
{
case QuestionType.OpenText:
@Html.Partial(“EditorTemplates/Types/TextViewModel”,Model)
打破
case QuestionType.SingleChoice:
@Html.Partial(“EditorTemplates/Types/SingleChoiceViewModel”,Model)
打破
}

@Html.Hidden(“ModelType”,Model.Questions.FirstOrDefault().GetType())
?Thnx,但隐藏字段中的值不是问题所在。。
@model PostModel

@Html.EditorFor(m => m.Questions)
@model QuestionViewModel
<div>
@Html.HiddenFor(m => m.Id)
@Html.Hidden("ModelType", Model.GetType())
@switch (Model.QuestionType)
{
    case QuestionType.OpenText:
        @Html.Partial("EditorTemplates/Types/TextViewModel", Model)
        break;
    case QuestionType.SingleChoice:
    @Html.Partial("EditorTemplates/Types/SingleChoiceViewModel", Model)
        break;

}
</div>