简体   繁体   English

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

[英]How to fill a list of objects that contain a list of string from view to controller in asp.net viewModels

I am using ASP.NET MVC5 I would like to store list of questions that contain: 我正在使用ASP.NET MVC5,我想存储包含以下内容的问题列表:

  1. The questions itself 问题本身
  2. The correct answer 正确答案
  3. List of wrong answers. 错误答案列表。

I am not sure what are the codes needed to accommodate my needs in the view to pass the required data from the form into the List<Question>jobQuestions in JobPostViewModel. 我不确定将视图中的所需数据从表单传递到List<Question>jobQuestions中的List<Question>jobQuestions所需的视图代码是什么。

These are the codes that I have done : 这些是我完成的代码:

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; }
}

I have tried many ways such as: 我尝试了许多方法,例如:

<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>

But it does not work as I expected. 但这并没有按我预期的那样工作。

I know could do something like : 我知道可以做类似的事情:

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;}
}

and the view I just need to do: 和我只需要做的看法:

@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>
}

But it is definitely not a good idea especially If I am going to collect hundred of questions. 但这绝对不是一个好主意,特别是如果我要收集数百个问题的话。

Thanks for any suggestion! 感谢您的任何建议!

Something like this would work, using your existing Question model. 使用您现有的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", })
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ASP.NET MVC中将Controller对象列表转换为Controller上的viewmodel - How can I convert a list of domain objects to viewmodels on the Controller in ASP.NET MVC 将对象列表从视图传递到asp.net中的控制器 - pass list of objects from view to controller in asp.net 如何在ASP.NET MVC中使用Razor View从对象列表中获取控制器中的单个对象 - How to get Single Object in Controller from List of Objects using Razor View in ASP.NET MVC ASP.Net MVC:如何使用列表 ViewData object 或从 controller 传递到视图的其他复杂对象 - ASP.Net MVC: How to use a List ViewData object or other complex objects passed from a controller to a view 使用 Html.BeginForm() ASP.NET MVC 将对象列表从控制器传递到视图时出错 - Error when passing list of objects from Controller to View using Html.BeginForm() ASP.NET MVC 如何从包含 asp.net 中确定字符串值的字符串列表中获取项目? - How can I get the items from a list of string that contain a determinate string value in asp.net? 从视图到控制器的ASP.NET MVC投递列表 - ASP.NET MVC posting list from view to controller 在ASP.NET MVC 4中将列表从Controller传递到View - Passing a list from Controller to View in ASP.NET MVC 4 从控制器获取int的列表以查看ASP.NET - Fetching List of int from controller to View ASP.NET 如何将项目列表从视图传递到控制器 (ASP.NET MVC 4) - How to Pass a List of Items from View to Controller (ASP.NET MVC 4)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM