Zapisywanie wielu obiektów z widoku MVC
Piszę swoją pierwszą aplikację MVC3, która jest prostą aplikacją do śledzenia zamówień. Chciałbym edytować zamówienie i szczegóły w tym samym czasie. Kiedy edytuję zamówienie, ActionResult dla edycji zwraca kolejność i powiązaną linię (używam również EF).
public ActionResult Edit(int id)
{
// Get the order with the order lines
var orderWithLines = from o in db.Orders.Include("OrderLines")
where o.ID == id
select o;
// Not sure if this is the best way to do this.
// Need to find a way to cast to "Order" type
List<Order> orderList = orderWithLines.ToList();
Order order = orderList[0];
// Use ViewData rather than passing in the object in the View() method.
ViewData.Model = order;
return View();
}
Kolejność i wiersze są wyświetlane bez problemu, ale gdy zapiszę stronę, nie otrzymuję żadnej z linii przekazywanych do kontrolera. Tylko zamówienie. Oto kod widoku.
@model OrderTracker.Models.Order
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
<fieldset>
<legend>Order</legend>
@Html.HiddenFor(model => model.ID)
@Html.HiddenFor(model => model.UserId)
<div>
@Html.LabelFor(model => model.OrderDate)
</div>
<div>
@Html.EditorFor(model => model.OrderDate)
</div>
<div>
@Html.LabelFor(model => model.Description)
</div>
<div>
@Html.EditorFor(model => model.Description)
</div>
<table>
<tr>
<th>
Description
</th>
<th>
Quantity
</th>
<th>
Weight
</th>
<th>
Price
</th>
<th></th>
</tr>
@foreach (var line in Model.OrderLines)
{
<tr>
<td>
@Html.EditorFor(modelItem => line.Description)
</td>
<td>
@Html.EditorFor(modelItem => line.Quantity)
</td>
<td>
@Html.EditorFor(modelItem => line.Weight)
</td>
<td>
@Html.EditorFor(modelItem => line.Price)
</td>
</tr>
}
</table>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
Czy mogę uzyskać pewne wskazówki dotyczące najlepszego sposobu zapisywania danych linii oraz danych zamówienia.
Dzięki.