4

我有一个带有 EF 的 ASP.NET MVC 4 互联网应用程序。

我的模型:订单和订单详情: 订单和订单详情

我的观点: 订单视图

问:如何在我的订单视图中显示我的订单详情记录? (有或没有 JS?;在 webgrid 或表格中?)

创建.cshtml

    <div class="editor-label">
        @Html.LabelFor(model => model.OrderDate)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.OrderDate)
        @Html.ValidationMessageFor(model => model.OrderDate)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.OrderNo)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.OrderNo)
        @Html.ValidationMessageFor(model => model.OrderNo)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Total)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Total)
        @Html.ValidationMessageFor(model => model.Total)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Shipping)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Shipping)
        @Html.ValidationMessageFor(model => model.Shipping)
    </div>
    @*OrderDetails Part - Works only on the Edit part*@
    <table>
        <tr>
            <th>Product</th>
            <th>Unit Price</th>
            <th>Quantity</th>
            <th>Discount</th>
        </tr>
    @foreach (OrderDetails item in Model.OrderDetails.OrderBy(d=>d.Products.ProductName))
    {
        <tr>
            <td>@item.Products.ProductName</td>
            <td>@item.UnitPrice</td>
            <td>@item.Quantity</td>
            <td>@item.Discount</td>
            <td>
                @Html.ActionLink("Edit", "Edit", "OrderDetails", new { id = item.OrderDetailId }, new { }) |
                @Html.ActionLink("Details", "Details", "OrderDetails", new { id = item.OrderDetailId }, new { }) |
                @Html.ActionLink("Delete", "Delete", "OrderDetails", new { id = item.OrderDetailId }, new { })
            </td>
        </tr>  
    }
    </table>

    <p>
        <input type="submit" value="Create" />
    </p>
4

1 回答 1

3

您应该使用视图模型来实现这一点。您不应该将您的业务模型直接暴露给视图。而是使用视图模型来提供另一个抽象级别。

所以,根本不要改变你的模型。为您的视图定义视图模型。我不会在视图模型中包含所有字段。

/* Viewmodel for your Order/Create page */
public class OrderCreate
{
    /* Attributes for your order */
    public string OrderDate          { get; set; }
    public string OrderNo            { get; set; }
    public string Shipping           { get; set; }
    ....
    ....

    /* List that will contain all details */
    public IList<ProductDetail> ProductDetails      { get; set; }

}

/* Viewmodel for each of the products */ 
public class ProductDetail
{
    public string Product            { get; set; }
    public string UnitPrice          { get; set; }
    public string Quantity           { get; set; }
    ....
    ....
}

现在在您的 GET 操作中,将此视图模型返回到您的视图而不是您的业务模型。

//
// GET: /Order/Create

public ActionResult Index()
{
    /* New viewmodel for your view */
    var viewModel = new OrderCreate();
    viewModel.ProductDetails = new List<ProductDetail>();

    /* Assuming the number of products is static */
    for(int i = 0; i < NUMBER_OF_PRODUCTS; i++)
    {
        viewModel.ProductDetails.Add( new ProductDetail() );
    }

    return View(viewModel);
}

使用此视图模型,您现在可以访问视图中填充的值。当您回发视图以发布操作时,请使用来自视图模型的数据在那里创建您的业务模型。

//
// POST: /Order/Create

[HttpPost]
public ActionResult Index(OrderCreate viewModel)
{
    if(ModelState.IsValid))
    {
        var model = new Order();

        //TODO: Populate model through viewmodel, loop viewModel.ProductDetails

        return RedirectToAction("Index");
    }

    // Model is not valid
    return View(viewMode);
}

一些建议,如果您厌倦了将视图模型映射到实际模型,请尝试AutoMapper

希望能帮助到你。

于 2012-09-22T12:57:27.657 回答