MVC - Edycja listy obiektów
Mam następujący układ klas w MVC:
public class ReportModel
{
List<SomeItem> items;
string value;
string anotherValue;
}
teraz tworzę silnie wpisany widok w MVC tego typu i wykonuję edytowalne pola tekstowe do edycji każdej wartości, jak również używam pętli foreach do wypełnienia pól tekstowych w celu edycji elementów na liście someitem.
kiedy przesyłam do metody httppost, pojedyncze wartości wracają w porządku w obiekcie reportmodel, ale lista nie jest zwracana w obiekcie. Jak to zrobić?
Kiedy mówię httppost, mam na myśli metodę, do której MVC wraca
[HttpPost]
public ActionResult EditReport(ReportModel report)
{
// Save the report in here after the update on the UI side
}
Zobacz kod do publikowania listy someitem
if (Model.items != null && Model.items.Count > 0)
{
for (int i = 0; i < Model.items.Count; i++)
{
<div class="editrow">
<div class="edititem">
<div class="editor-label">
@Html.LabelFor(m => m.items.ElementAt(i).propertyOne)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.items.ElementAt(i).propertyOne)
@Html.ValidationMessageFor(m => m.items.ElementAt(i).propertyOne)
</div>
</div>
<div class="edititem">
<div class="editor-label">
@Html.LabelFor(m => m.items.ElementAt(i).propertyTwo)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.items.ElementAt(i).propertyTwo)
@Html.ValidationMessageFor(m => m.items.ElementAt(i).propertyTwo)
</div>
</div>
<div class="edititem">
<div class="editor-label">
@Html.LabelFor(m => m.items.ElementAt(i).propertyThree)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.items.ElementAt(i).propertyThree)
@Html.ValidationMessageFor(m => m.items.ElementAt(i).propertyThree)
</div>
</div>
</div>
}
}