在MVVM详细信息视图中使用附加表

时间:2013-06-18 09:16:02

标签: asp.net-mvc model-view-controller mvvm detailsview asp.net-mvc-controller

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

在最后一个表的底部,我想实现一个单独的表,其中包含Sales_Order_Line表中的Product_ID,Description,Price,Quantity和Total,但不知道如何添加它,因为我只能使用@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; }
}

以下是Details控制器的代码:

        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>

1 个答案:

答案 0 :(得分: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>