Как передать List <model> контроллеру в MVC 4
У меня есть 2 модели: Вопрос и ответ, такие как ниже, я хочу отправить модель списка в View, и при отправке формы я отправляю модель списка в контроллер, но в Action UpdateQuestion можно получить только список вопросов, но список ответа не было. Можете ли вы объяснить и показать мне, как получить список ответов на каждый вопрос, когда я отправляю форму
public class Question
{
[Key]
public int Id { get; set; }
[ForeignKey("QuestionType")]
public int QuestionTypeId { get; set; }
public virtual QuestionType QuestionType { get; set; }
[ForeignKey("Field")]
public int FieldId { get; set; }
public virtual Field Field { get; set; }
public string Brief { get; set; }
public bool IsGroup { get; set; }
[ForeignKey("QuestionGroup")]
public int? QuestionGroupId { get; set; }
public virtual QuestionGroup QuestionGroup { get; set; }
public int Priority { get; set; }
public int Order { get; set; }
public virtual ICollection<Answer> Answers { get; set; }
}
а также:
public class Answer
{
[Key]
public Int32 Id { get; set; }
[Column(TypeName = "ntext")]
[MaxLength]
public string Content { get; set; }
[ForeignKey("Question")]
public int QuestionId { get; set; }
public virtual Question Question { get; set; }
public float Mark { get; set; }
public int Priority { get; set; }
}
У меня есть контроллер Index для передачи списка вопросов для просмотра:
public ActionResult Index()
{
ApplicationDbContext db = new ApplicationDbContext();
var listQuestion = db.Questions.ToList();
return View(listQuestion);
}
[HttpPost]
public ActionResult UpdateQuestion(string submit, List<Question> Questions)
{
...
return RedirectToAction("Index");
}
И на виду:
@model List<Question>
@{
int i = 0;
int j = 0;
}
@using (Html.BeginForm("UpdateQuestion", "TestRoom"))
{
<ul>
@foreach(var question in Model)//Question
{
<li>
@Html.Hidden("Questions["+i+"].Id", question.Id)
@{i++;}
@Html.Raw(question.Brief)
<ul>
@foreach (var answers in question.Answers)
{
<li>@Html.RadioButton("Questions["+i+"]_Answers["+j+"]",answers.Id)
@Html.Raw(answers.Content)
@{j++;}
</li>
}
@{j = 0;}
</ul>
</li>
}
</ul>
<div class="aq-button-panel">
<button type="submit" value="Finish" name="submit"><i class="icon-pencil"></i>Submit</button>
<button type="submit" value="Back" name="submit">Go Next <i class="icon-arrow-left"></i></button>
<button type="submit" value="Next" name="submit">Go Back <i class="icon-arrow-right"></i></button>
</div>
}