从MVC视图保存多个对象

use*_*207 3 c# asp.net-mvc-3

我正在编写我的第一个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();            
    }
Run Code Online (Sandbox Code Playgroud)

订单和行显示没有问题,但是当我保存页面时,我没有将任何行传递回控制器.只有订单.这是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>    
}
Run Code Online (Sandbox Code Playgroud)

我能否就保存生产线数据和订单数据的最佳方法获得一些指导.

谢谢.

Pra*_*nam 7

您遇到的问题与ICollection<T>控件生成的名称有关.以下是Phil Haack详细讨论和他的解决方案(就@Html扩展方法而言 ;从他博客文章末尾给出的链接下载示例项目).这篇文章的目标是MVC/MVC2; 但它仍然适用于MVC3.

另外,如果你不想遵循黑客,你可以选择一个EditorTemplate对你的OrderLine实体模型.

这是步骤.

1)在(Views ->Shared -> EditorTemplates -> OrderLine.cshtml)下创建编辑器模板创建名为EditorTemplatesunder 的文件夹很重要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>
Run Code Online (Sandbox Code Playgroud)

3)使用此代码编辑您的视图(请注意我已用于EditorForOrderLines集合)

@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>    
}
Run Code Online (Sandbox Code Playgroud)

4)现在回帖后你会看到值

在此输入图像描述