c# - 从 MVC View 保存多个对象

标签 c# asp.net-mvc-3

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

顺序和行显示没有问题,但是当我保存页面时,我没有将任何行传回 Controller 。只有订单。这是查看代码。

    @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 )

enter image description here

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) 使用此代码编辑您的 View (请注意,我已将 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) 现在回发时您会看到值

enter image description here

关于c# - 从 MVC View 保存多个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11167538/

相关文章:

c# - 如何检查列是否已经是外键?

c# - Entity Framework 延迟加载不适用于其他线程

c# - 如何隐藏 miniprofiler?

c# - 在 Windows Phone 7 中创建一个简单的测验应用程序

c# - 调试在 NUnit 下运行的测试

c# - WorkFlow Foundation 4 WorkflowApplication 完成的输出为空白

asp.net-mvc-3 - MVC3 HTMLHelper 默认值

c# - MVC3 中更新局部 View 的 SetTimeout 随机执行

asp.net-mvc - MultiSelectList SelectedValues,在 mvc3 中不起作用

javascript - 客户端 Microsoft Word 到 PDF 的转换——如何在 MVC3 中进行?