如何在asp.net viewModels中填充包含从视图到控制器的字符串列表的对象列表

本文关键字:列表 视图 控制器 字符串 对象 包含 asp net viewModels 填充 | 更新日期: 2023-09-27 18:10:24

我使用的是ASP。净MVC5我想存储包含以下内容的问题列表:

  1. 问题本身
  2. 正确答案
  3. 错误答案列表。

我不确定在视图中容纳我的需求所需的代码是什么,以便将所需的数据从表单传递到JobPostViewModel中的List<Question>jobQuestions

这些是我所做的代码:

public class JobPostViewModel
{
    public class Question
    {
        public string theQuestion { get; set; }
        public string RightAnswer { get; set; }
        public List<string> WrongAnswers { get; set; }
    }
    public List<Question>jobQuestions { get; set; }
}

我试过很多方法,例如:

<div class="form-group">
     <div class="col-md-10">
          @Html.TextBoxFor(model => model.jobQuestions.FirstOrDefault().theQuestion, new { @class = "form-control", })
          @Html.TextBoxFor(model => model.jobQuestions.FirstOrDefault().RightAnswer, new { @class = "form-control", })
          @Html.TextBoxFor(model => model.jobQuestions.FirstOrDefault().WrongAnswers, new { @class = "form-control", })
          @Html.TextBoxFor(model => model.jobQuestions.FirstOrDefault().WrongAnswers, new { @class = "form-control", })
          @Html.TextBoxFor(model => model.jobQuestions.FirstOrDefault().WrongAnswers, new { @class = "form-control", })
</div>

但是它没有像我预期的那样工作。

我知道可以这样做:

public class JobPostViewModel
{
    public string question1 { get; set;}
    public string correctAnswer1 { get; set;}
    public List<string>IncorrectAnswers1 { get; set;}
    public string question2 { get; set;}
    public string correctAnswer2 { get; set;}
    public List<string>IncorrectAnswers2 { get; set;}
}

和我只需要做的视图:

@using (Html.BeginForm()) 
{
    @Html.TextBoxFor(model => model.question1, new { @class = "form-control", })
    @Html.TextBoxFor(model => model.correctAnswer1 , new { @class = "form-control", })
    @Html.TextBoxFor(model => model.IncorrectAnswers1 , new { @class = "form-control", })
    @Html.TextBoxFor(model => model.IncorrectAnswers1 , new { @class = "form-control", })
    @Html.TextBoxFor(model => model.IncorrectAnswers1 , new { @class = "form-control", })
    @Html.TextBoxFor(model => model.question2, new { @class = "form-control", })
    @Html.TextBoxFor(model => model.correctAnswer2 , new { @class = "form-control", })
    @Html.TextBoxFor(model => model.IncorrectAnswers2 , new { @class = "form-control", })
    @Html.TextBoxFor(model => model.IncorrectAnswers2 , new { @class = "form-control", })
    @Html.TextBoxFor(model => model.IncorrectAnswers2 , new { @class = "form-control", }) 
    <p>
       <input type="submit" value="Save" />
   </p>
}

但这绝对不是一个好主意,特别是如果我要收集数百个问题。

谢谢你的建议!

如何在asp.net viewModels中填充包含从视图到控制器的字符串列表的对象列表

使用您现有的Question模型,这样就可以工作。

@using (Html.BeginForm()) 
{
    @Html.TextBoxFor(model => model.question, new { @class = "form-control", })
    @Html.TextBoxFor(model => model.correctAnswer , new { @class = "form-control", })
    @for (int i = 0; i < Model.WrongAnswers.Count; i++)
    {
        @Html.TextBoxFor(model => model.WrongAnswers[i], new { @class = "form-control", })
    }