[英]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,我想存储包含以下内容的问题列表:
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.