MVC Jak przekazać listę obiektów za pomocą metody POST elementów listy
Chcę opublikować listę elementów do kontrolera z widoku Razor, ale otrzymuję listę obiektów jako null Moja struktura klasy to
Model:
List<Subjects> modelItem
class Subjects
{
int SubId{get;set;}
string Name{get;set;}
List<Students> StudentEntires{get;set;}
}
class StudentEntires
{
int StudId{get;set;}
string Name{get;set;}
int Mark{get;set;}
}
Sam model jest listą elementów, a każdy element zawiera również listę elementów potomnych. Przykładowy model to lista przedmiotów, a każdy temat zawiera listę uczniów i chcę wprowadzić znak dla każdego ucznia
Mój widok jest jak
@model IList<Subjects>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
if (Model.Count > 0)
{
@for (int item = 0; item < Model.Count(); item++)
{
<b>@Model[item].Name</b><br />
@foreach (StudentEntires markItem in Model[item].StudentEntires)
{
@Html.TextBoxFor(modelItem => markItem.Mark)
}
}
<p style="text-align:center">
<input type="submit" class="btn btn-primary" value="Update" />
</p>
}
}
I w kontrolerze
[HttpPost]
public ActionResult OptionalMarks(int Id,ICollection<Subjects> model)
{
//BUt my model is null. Any idea about this?
}