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

时间:2014-06-26 08:28:08

标签: c# asp.net .net asp.net-mvc viewmodel

我正在使用ASP.NET MVC5 我想存储包含以下内容的问题列表:

  1. 问题本身
  2. 正确答案
  3. 错误答案清单。
  4. 我不确定在视图中将所需数据从表单传递到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>
    }
    

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

    感谢您的任何建议!

1 个答案:

答案 0 :(得分:0)

使用现有的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", })
    }