如何模拟绑定实现接口的类?(How to model bind a class that imple

2019-06-24 23:38发布

该模型绑定工作正常,直到我对以下类别的基础上实现的接口:

public class QuestionAnswer : IQuestionAnswer
    {

        public Int32 Row_ID { get; set; }
        public Int32 Column_ID { get; set; }
        public String Value { get; set; }

    }

    public class HiddenAnswer : IHiddenAnswer
    {

        public Int32 Hidden_Field_ID { get; set; }
        public String Hidden_Field_Value { get; set; }

    }

    public class SurveyAnswer : ISurveyAnswer
    {

        public string SessionID { get; set; }

        public List<IQuestionAnswer> QuestionAnswerList { get; set; }

        public List<IHiddenAnswer> HiddenAnswerList { get; set; }

        public SurveyAnswer()
        {
            QuestionAnswerList = new List<IQuestionAnswer>();
            HiddenAnswerList = new List<IHiddenAnswer>();
        }
    }

现在,接口在那里,我得到一个500 (Internal Server Error)

我使用模型绑定的JavaScript如下:

$('#submitbutton').click(function () {

            var answers = new Array();
            var hiddenfields = new Array();

            var formname = "#" + $("#formname").val();

            $(':input', formname).each(function () {

                if ($(this).is(":text") || $(this).is(":radio") || $(this).is(":checkbox")) 
                {
                    var answerObject = {
                        Column_ID: $(this).attr('data-column_id'),
                        Row_ID: $(this).attr('data-row_id'),
                        Value: $(this).attr('data-theValue')
                    };

                    answers.push(answerObject);
                }

                else if($(this).is(":hidden")) {
                    var hiddenObject = 
                    {
                        Hidden_Field_ID: $(this).attr('data-hidden_field_id'),
                        Hidden_Field_Value: $(this).attr('data-hidden_field_value')
                    }

                    hiddenfields.push(hiddenObject);
                }
            });

            $('textarea', formname).each(function () {
                var answerObject = {
                    Column_ID: $(this).attr('data-column_id'),
                    Row_ID: $(this).attr('data-row_id'),
                    Value: $(this).val(),
                };

                answers.push(answerObject);
            });

            var allAnswers = {
                SessionID: 0,
                QuestionAnswerList: answers,
                HiddenAnswerList: hiddenfields
            }

            postForm(allAnswers);
        });

控制器中的操作是这样的:

 [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SubmitSurvey(SurveyAnswer answers)
        {
            // Dette tillader CORS
            Response.AppendHeader("Access-Control-Allow-Origin", "*");

            bc.SaveSurvey(answers);

            return null;
        }

我究竟做错了什么?

Answer 1:

我究竟做错了什么?

你不能指望模型绑定知道,当它遇到IQuestionAnswer您接口SurveyAnswer视图模型应该使用QuestionAnswer的类型。 这是很好,你已经宣布该接口的实现,但模型绑定有没有关于它的线索。

所以,你将不得不写了一个自定义的模型绑定IQuestionAnswer接口(同为IHiddenAnswer接口),并注明你要使用哪一个实现:

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;
    }
}

这将在您注册Application_Start

ModelBinders.Binders.Add(typeof(IQuestionAnswer), new QuestionAnswerModelBinder());


文章来源: How to model bind a class that implements an interface?