本文介绍了如何绑定模型实现接口的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
该模型绑定工作正常,直到我上实现以下类的顶部接口:
公共类问题答案:IQuestionAnswer
{ 公众的Int32 ROW_ID {搞定;组; }
公众的Int32 COLUMN_ID {搞定;组; }
公共字符串值{搞定;组; } } 公共类HiddenAnswer:IHiddenAnswer
{ 公众的Int32 Hidden_Field_ID {搞定;组; }
公共字符串Hidden_Field_Value {搞定;组; } } 公共类SurveyAnswer:ISurveyAnswer
{ 公共字符串的SessionID {搞定;组; } 公开名单< IQuestionAnswer> QuestionAnswerList {搞定;组; } 公开名单< IHiddenAnswer> HiddenAnswerList {搞定;组; } 公共SurveyAnswer()
{
QuestionAnswerList =新的List< IQuestionAnswer>();
HiddenAnswerList =新的List< IHiddenAnswer>();
}
}
现在其接口有,我收到了 500(内部服务器错误)
这是我使用模型绑定的javascript如下:
$('#提交按钮')。点击(函数(){ VAR答案=新的Array();
VAR hiddenfields =新的Array(); 变种表格名称=#+ $(#表格名称)VAL(); $(':输入',表格名称)。每个(函数(){ 如果($(本)。是(:文本)|| $(本)。是(:电台)|| $(本)。是(:复选框))
{
VAR answerObject = {
COLUMN_ID:$(本).attr('数据COLUMN_ID'),
ROW_ID:$(本).attr('数据ROW_ID'),
价值:$(本).attr('数据theValue')
}; answers.push(answerObject);
} 否则,如果($(本)。是(:隐藏)){
VAR hiddenObject =
{
Hidden_Field_ID:$(本).attr('数据hidden_field_id'),
Hidden_Field_Value:$(本).attr('数据hidden_field_value')
} hiddenfields.push(hiddenObject);
}
}); $('textarea的,表格名称)。每个(函数(){
VAR answerObject = {
COLUMN_ID:$(本).attr('数据COLUMN_ID'),
ROW_ID:$(本).attr('数据ROW_ID'),
价值:$(本).VAL()
}; answers.push(answerObject);
}); VAR allAnswers = {
的SessionID:0,
QuestionAnswerList:答案,
HiddenAnswerList:hiddenfields
} postForm(allAnswers);
});
控制器中的操作是这样的:
的[AcceptVerbs(HttpVerbs.Post)
公众的ActionResult SubmitSurvey(SurveyAnswer答案)
{
//德特tillader CORS
Response.AppendHeader(访问控制允许原产地,*); bc.SaveSurvey(答案); 返回null;
}
我在做什么错了?
解决方案
You cannot expect the model binder to know that when it encounters the IQuestionAnswer
interface on your SurveyAnswer
view model it should use the QuestionAnswer
type. It's nice that you have declared this implementation of the interface but the model binder has no clue about it.
So you will have to write a custom model binder for the IQuestionAnswer
interface (same for the IHiddenAnswer
interface) and indicate which implementation do you want to be used:
public class QuestionAnswerModelBinder : DefaultModelBinder
{
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
var type = typeof(QuestionAnswer);
var model = Activator.CreateInstance(type);
bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, type);
return model;
}
}
which will be registered in your Application_Start
:
ModelBinders.Binders.Add(typeof(IQuestionAnswer), new QuestionAnswerModelBinder());
这篇关于如何绑定模型实现接口的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!