0
我已阅读一些帖子,但我仍然觉得很难解决这个问题。我的问题是,我的行动只读取绑定列表中的一些值。这是我的列表发送到视图:模型绑定列表MVC3不读取值
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个答案......奇怪。任何人都知道这是为什么发生?
是的,我刚刚在一分钟前意识到,一边看着生成的标记。而且,我不知道为什么,第一组中的if语句以某种方式制约了绑定,所以我做的是将一个列表中的正常问题分开,并在另一个列表中提出问题,而不是将它们全部合并为一个过滤。这使绑定正常工作。 –