0

我已经阅读了一些帖子,但我仍然觉得很难解决这个问题。我的问题是我的操作只从绑定列表中读取一些值。这就是我将列表发送到视图的方式:

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 个答案……很奇怪。任何人都知道为什么会这样?

在此处输入图像描述

4

1 回答 1

2

您有多个输入控件在同一个表单中具有相同的名称。

建议的问题是所有问题的子集,因此它们在同一表格上重复两次。这将抛出 ModelBinder,因此您的接收操作可能只看到未重复的问题。

于 2012-05-17T15:50:09.307 回答