我正在编写我的第一个 MVC3 应用程序,它是一个简单的订单跟踪应用程序。我想同时编辑订单和详细信息。当我编辑订单时,编辑的 ActionResult 返回订单和相关行(我也在使用 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();
}
订单和行显示没有问题,但是当我保存页面时,我没有将任何行传回控制器。只有顺序。这是查看代码。
@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>
}
我能否获得一些关于保存线路数据和订单数据的最佳方式的指导。
谢谢。