2

目前我有一个 OrderConfirmationController,它有一个显示记录的详细信息视图。在详细信息视图的底部,我想从另一个表中添加 5 个字段,这些字段将显示在视图中,但不知道如何添加它们。

在最后一个表的底部,我想实现一个单独的表,其中包含来自 Sales_Order_Line 表的 Product_ID、描述、价格、数量和总计,但不知道如何添加它,因为我只能使用 @model 引用一个表视图的开始。

谢谢。这是我创建的新类的代码:

class OrderConfirmation
{
    Sales_Order Bill_to_Customer_Name { get; set; }
    Sales_Order Bill_to_Address { get; set; }
    Sales_Order Bill_to_Address_2 { get; set; }
    Sales_Order Bill_to_City { get; set; }
    Sales_Order Bill_to_County { get; set; }
    Sales_Order Bill_to_Post_Code { get; set; }
    Sales_Order Bill_to_Country_Region_Code { get; set; }
    Sales_Order Ship_to_Contact_Name { get; set; }
    Sales_Order Ship_to_Address { get; set; }
    Sales_Order Ship_to_Address_2 { get; set; }
    Sales_Order Ship_to_City { get; set; }
    Sales_Order Ship_to_County { get; set; }
    Sales_Order Ship_to_Post_Code { get; set; }
    Sales_Order Ship_to_Country_Region_Code { get; set; }

    Sales_Order_Line Product_ID { get; set; }
    Sales_Order_Line Description { get; set; }
    Sales_Order_Line Price { get; set; }
    Sales_Order_Line Quantity { get; set; }
    Sales_Order_Line Total { get; set; }
}

这是我的详细信息控制器代码:

        public ActionResult Details(string documentNo, int documentType)
    {
        ViewBag.Sales_Order_Line = Sales_Order_Line.Product_ID;
        ViewBag.Sales_Order_Line = Sales_Order_Line.Description;
        ViewBag.Sales_Order_Line = Sales_Order_Line.Price;
        ViewBag.Sales_Order_Line = Sales_Order_Line.Quantity;
        ViewBag.Sales_Order_Line = Sales_Order_Line.Total;

        // Compound key is used here so require both fields to be included in the SQL.
        Sales_Order confirmOrder = _data.Sales_Orders
           .Where(x => x.Document_Type == documentType && x.Document_No_ == documentNo)
           .FirstOrDefault();
        return View(confirmOrder);           
    }

这是我的观点:

 @model SPR.Titanium.MultiChannel.ManagementConsolePortal.Models.OrderConfirmation
<link href="~/Content/themes/ui-lightness/jquery-ui-1.10.3.custom.css" rel="stylesheet" />

@section Scripts
{
@Scripts.Render("~/bundles/jqueryui");
<script src="~/js/jquery-1.9.1.js"></script>
<script src="~/js/jquery-ui-1.10.3.custom.js"></script>
<script>
    $(function () {
        $("#accordion").accordion();
    })
</script>

}

@{
ViewBag.Title = "Order Confirmation Details";
}

<h2>Order Confirmation</h2>

@using (Html.BeginForm())
{
<div id="accordion">
    <div class="editor-field">
        Billing Details:
    </div>
    <div class="editor-field">
        <table style="width:100%">
            <tr>

                <th style="width:20%">Billing Name</th>
                <td style="width:30%">@Html.EditorFor(model =>         model.Bill_to_Customer_Name)</td>
                <th style="width:20%">Address</th>
                <td style="width:30%">@Html.EditorFor(model => model.Bill_to_Address).</td>
                <th style="width:20%">Address 2</th>
                <td style="width:30%">@Html.EditorFor(model => model.Bill_to_Address_2).</td>
            </tr>
            <tr>
                <th style="width:20%">City</th>
                <td style="width:30%">@Html.EditorFor(model => model.Bill_to_City)</td>
                <th style="width:20%">County</th>
                <td style="width:30%">@Html.EditorFor(model => model.Bill_to_County).</td>
                <th style="width:20%">Post Code</th>
                <td style="width:30%">@Html.EditorFor(model => model.Bill_to_Post_Code)</td>
            </tr>
            <tr>
                <th style="width:20%">Country</th>
                <td style="width:30%">@Html.EditorFor(model => model.Bill_to_Country_Region_Code)</td>
            </tr>

        </table>
    </div>
    <div class="editor-label">
        Delivery Details
    </div>
    <div class="editor-field" aria-readonly="true">
        <table style="width:100%">
            <tr>
                <th style="width:20%">Delivery Name</th>
                <td style="width:30%">@Html.EditorFor(model => model.Ship_to_Contact_Name)</td>
                <th style="width:20%">Address</th>
                <td style="width:30%">@Html.EditorFor(model => model.Ship_to_Address)</td>
                <th style="width:20%">Address 2</th>
                <td style="width:30%">@Html.EditorFor(model => model.Ship_to_Address_2)</td>
                </tr>
            <tr>
                <th style="width:20%">City</th>
                <td style="width:30%">@Html.EditorFor(model => model.Ship_to_City)</td>
                <th style="width:20%">County</th>
                <td style="width:30%">@Html.EditorFor(model => model.Ship_to_County)</td>
                <th style="width:20%">Post Code</th>
                <td style="width:30%">@Html.EditorFor(model => model.Ship_to_Post_Code)</td>
            </tr>
            <tr>
                <th style="width:20%">Country</th>
                <td style="width:30%">@Html.EditorFor(model => model.Ship_to_Country_Region_Code)</td>
            </tr>
            </table>
    </div>
    <div>
        @{
var line = (Sales_Order_Line)ViewBag.Sales_Order_Line;

    </div>
</div>   

}

<div>
@Html.ActionLink("Return to Sales Order List", "Index")
</div>
4

1 回答 1

0

我只是注意到我的第一个想法不会像我想象的那样奏效。但是您可以使用 ViewBag 将附加值传递给视图:

创建一个具有两个属性的新类,一个是 Sales_Order 类型,另一个是包含另一个表中的数据的类。使用这个新类作为你的@model,你可以同时访问它们。

class DetailsViewModel
{
    Sales_Order SalesOrder { get; set; }
    Sales_Order_Line { get; set; }
}

在您的控制器中:

public ActionResult Details(string documentNo, int documentType)
{
    ViewBag.Sales_Order_Line = // get Sales_Order_Line object here

    ... // the rest of the code as before
}

在您看来:

<div>
    @{
        var line = (Sales_Order_Line)ViewBag.Sales_Order_Line;
        ...
    }
</div>
于 2013-06-18T09:51:00.803 回答