我正在使用复杂的ViewModel来构建调查表的系统存在问题。提交表单后,返回的ViewModel为空。
我见过有关ViewModel的参数名称与ViewModel本身不同的文章,以及ViewModel中的一些参数名称引起问题的帖子,但是我看不到任何不愉快的地方。
有什么想法吗?
谢谢 西蒙
ViewModel看起来像这样:
public class SurveysViewModel
{
public Survey SurveyDetails { get; set; }
[AdditionalMetadata("HideLabel", true)]
public List<QuestionSet> SurveyQuestions { get; set; }
}
其中的类如下:
public class Survey
{
public int Id { get; set; }
public string SurveyName { get; set; }
public int IntroId { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public bool Active { get; set; }
public string IntroName { get; set; }
public string Intro { get; set; }
public string Conclusion { get; set; }
}
public class QuestionSet
{
public int? SubQuestionOf { get; set; }
public int Order { get; set; }
public bool RepeatPerUser { get; set; }
public int Id { get; set; }
public string QuestionText { get; set; }
public string Q_Text { get; set; }
public string SubText { get; set; }
public string TableTitle { get; set; }
public List<Answer> Answers { get; set; }
public string Format { get; set; }
public string OptionsGroup { get; set; }
public string DBViewName { get; set; }
public string Header { get; set; }
public int? Minimum { get; set; }
public int? Maximum { get; set; }
public int? Interval { get; set; }
public int SelectedOption { get; set; }
public bool Selected { get; set; }
}
控制器中的Post方法如下:
[HttpPost]
[ValidateAntiForgeryToken]
[ActionName("CompleteSurvey")]
public ActionResult CompleteSurvey_Post(SurveysViewModel vm)
{
if (ModelState.IsValid)
{
//Do something in here
}
}
但是ViewModel(vm)将这两部分显示为空。
我应该补充说,问题集中的问题是多种类型的(例如,下拉列表或范围输入),例如,它们继承自QuestionSet:
public class QuestionSet_DropDown : QuestionSet
{
public QuestionSet_DropDown(QuestionSet question)
{
Header = question.Header;
Id = question.Id;
QuestionText = question.QuestionText;
Q_Text = question.Q_Text;
AllowMultiSelect = false;
}
public List<Answer> Options { get; set; }
public bool AllowMultiSelect { get; set; }
}
@model SurveysViewModel
<h1>Survey Page</h1>
<form action="/PublicAuthSurvey/CompleteSurvey" method="post" class="form-horizontal">
@Html.AntiForgeryToken()
<div class="survey">
@{ var questions = Model.SurveyQuestions; }
@Html.EditorForModel(questions)
</div>
<input class="btn btn-lg btn-success" type="submit" value="Submit Results" />
</form>
答案 0 :(得分:0)
感谢到目前为止的评论。我终于设法到达那里。
我混合使用了嵌套的复杂类型,继承和自定义编辑器模板,当您查看页面上的标记时,创建的某些输入未正确映射回模型。
如果其他人正在阅读该文章以寻找解决方案,请使用开发人员工具检查编辑器模板已创建的输入的名称,并确保它们正确地映射回您的模型。对我来说,一种类型的映射不正确,因此整个模型都被返回为null。
经过多次脱毛,我想我就在那里!
谢谢 西蒙