我已经阅读了一些帖子,但我仍然觉得很难解决这个问题。我的问题是我的操作只从绑定列表中读取一些值。这就是我将列表发送到视图的方式:
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 个答案……很奇怪。任何人都知道为什么会这样?