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();            
    }

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

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

我能否获得一些关于保存线路数据和订单数据的最佳方式的指导。

谢谢。

4

3 回答 3

7

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

或者,如果您不想遵循 hack,您可以EditorTemplate为您的OrderLine实体模型选择一个。

以下是步骤。

1)在( )下创建Editor模板Views ->Shared -> EditorTemplates -> OrderLine.cshtml重要的是创建一个名为的文件夹EditorTemplatesShared模板名称要与要创建模板的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)使用此代码编辑您的视图(请注意,我已用于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>    
}

4)现在在回发时,您将看到这些值

在此处输入图像描述

于 2012-06-24T00:40:39.393 回答
0

如果您查看它生成的原始 html,则问题出在您的 foreach 上,它不会为每个订单行生成唯一的 id,因此在回发表单时将无法绑定模型。

将 foreach 更改为 for 循环,然后使用索引引用每个订单行。这将允许在表单中生成唯一 ID,并允许您在回发模型时将其绑定到模型。

例如

@for (var counter = 0; counter < Model.OrderLines.Count(); counter++)

    { 
        <tr>
            <td>
                @Html.EditorFor(modelItem => Model.OrderLines[counter].Description)
            </td> 
            <td>
                @Html.EditorFor(modelItem => Model.OrderLines[counter].Quantity)
            </td> 
            <td>
                @Html.EditorFor(modelItem => Model.OrderLines[counter].Weight)
            </td> 
            <td>
                @Html.EditorFor(modelItem => Model.OrderLines[counter].Price)
            </td>
        </tr>
    }
    </table>
于 2012-06-23T15:28:06.957 回答
0

使用 MVC 应该相当简单,因为该框架旨在将表单转换为模型。

[HttpGet]
public ActionResult Edit(int id)
{
    // (you can probably rewrite this using a lambda
    var orderWithLines = from o in db.Orders.Include("OrderLines")
                         select o;

    // Use ViewData rather than passing in the object in the View() method.
    ViewData.Model = orderWithLines.FirstOrDefault(x => x.ID = id);
    return View();
}

[HttpPost]
public ActionResult Edit(OrderTracker.Models.Order model)
{
    if (ModelState.IsValid)
    {
        // call the service layer / repository / db to persist the object graph
        _service.Save(model); // this assumes your view models are the same as your domain
    }
}
于 2012-06-23T06:59:12.393 回答