我正在编写我的第一个MVC3应用程序,它是一个简单的订单跟踪应用程序。我想同时编辑订单和详细信息。当我编辑订单时,Edit的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>
}
我可以获取有关保存行数据和订单数据的最佳方法的一些指导吗?
谢谢。
最佳答案
您面临的问题与ICollection<T>
控件生成的名称有关。这是他的detailed discussion by Phil Haack和解决方案(根据@Html扩展方法;从他的博客文章结尾处的链接下载示例项目)。该职位针对MVC / MVC2;但是它仍然适用于MVC3。
另外,如果您不想遵循该技巧,则可以为EditorTemplate
实体模型选择OrderLine
。
步骤如下。
1)在(Views ->Shared -> EditorTemplates -> OrderLine.cshtml
)下创建编辑器模板
在EditorTemplates
下创建一个名为Shared
的文件夹很重要,模板名称应与您要为其创建模板的EntityModel相同;因此名称OrderLine.cshtml
)
2)OrderLine.cshtml的代码
@model OrderTracker.Models.OrderLine
@{
Layout = null;
}
<!DOCTYPE html>
@Html.HiddenFor(modelItem => Model.id)
<tr>
<td>
@Html.EditorFor(modelItem => Model.Description)
</td>
<td>
@Html.EditorFor(modelItem => Model.Quantity)
</td>
<td>
@Html.EditorFor(modelItem => Model.Weight)
</td>
<td>
@Html.EditorFor(modelItem => Model.Price)
</td>
</tr>
3)使用此代码编辑视图(请注意,我已经使用
EditorFor
进行OrderLines收集)@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>
<div>
<table>
<tr>
<th>
Description
</th>
<th>
Quantity
</th>
<th>
Weight
</th>
<th>
Price
</th>
</tr>
@Html.EditorFor(model => model.OrderLines)
</table>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
4)现在回发,您将看到值
关于c# - 从MVC View 保存多个对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11167538/