我已经阅读了一些帖子,但我仍然觉得很难解决这个问题。我的问题是我的操作只读取绑定列表中的一些值。这就是我将列表发送到视图的方式:
public ActionResult RegisterSurvey()
{
RegisterSurveyModel model = new RegisterSurveyModel();
var questions = new List<QuestionModel>();
var survey = EFSurvey.Survey.FirstOrDefault();
survey.QuestionSurvey
.Where(x => x.AuditingDeleted == false)
.Where(x => x.Active == true).ToList().ForEach((item) =>
{
var questionModel = new QuestionModel();
ModelCopier.CopyModel(item, questionModel);
questionModel.Answer = string.Empty;
questions.Add(questionModel);
});
model.Questions = questions;
return View(model);
}
这是我的模特:
public class RegisterSurveyModel
{
public List<QuestionModel> Questions { get; set; }
}
public class QuestionModel
{
public int QuestionSurveyID { get; set; }
public string Question { get; set; }
public string Answer { get; set; }
public bool Suggestion { get; set; }
}
这是我的观点:
<div class="SiteSurveyContainer">
@using (Html.BeginForm())
{
<div class="SurveyUp">
@for (int i = 0; i < Model.Questions.Count(); i++)
{
if (!Model.Questions[i].Suggestion)
{
<p>@Model.Questions[i].Question</p>
@Html.HiddenFor(x => Model.Questions[i].QuestionSurveyID);
@Html.TextBoxFor(x => Model.Questions[i].Answer, new { @class = "surveyBox" });
}
}
</div>
<div class="SurveyBottom">
<div class="line">
</div>
<p>
Suggestions</p>
@for (int i = 0; i < Model.Questions.Where(x => x.Suggestion == true).Count(); i++)
{
@Html.HiddenFor(x => Model.Questions[i].QuestionSurveyID);
@Html.TextAreaFor(x => Model.Questions[i].Answer, new { @class = "surveyTextArea" })
}
</div>
<div class="surveyBottomButton">
<input type="submit" value="Submit Results" />
</div>
}
到目前为止一切顺利。无论如何,当我回答所有调查问题时,我只得到前4个答案......很奇怪。任何人都知道为什么会这样?
答案 0 :(得分:2)
您有多个输入控件,在同一表单中具有相同的名称。
建议的问题是所有问题的子集,因此它们在同一表格上重复两次。这会甩掉ModelBinder,所以你的接收动作可能只是看到了没有重复的问题。