2

除了我视图中的详细信息行之外,我还想显示一些汇总数据,例如“计数”和“总和”。我有一个拣货清单表,除了显示按 SKU 组织的清单行之外,我还想显示每个 SKU 的总数。

我的 PickingList 模型如下所示:

public partial class PickingList
{
    public int Id { get; set; }
    [ForeignKey("List_Header")]
    public int TransID { get; set; }
    public string SKU { get; set; }
    public int ColorId { get; set; }
    public double Qty { get; set; }
    public decimal Price { get; set; }

    public virtual List_Header List_Header { get; set; }
}

我将 TransID 传递给列表的控制器是:

    public ActionResult ListShipment(int transid)
    {
        var mylist = db.PickingLists.Where(p => p.TransID == transid);
        mylist = mylist.OrderBy(p => p.Id);
        return View(mylist.ToList());
    }

最后,我想要实现的是这样的:

SKU           Qty              Color

61009         12 pieces
               3               Blue
               5               Red
               4               Green

61011         10 pieces
               6               Blue
               4               Red

我尝试了以下 View 代码块,但不知道在哪里放置聚合(求和)函数:

@foreach (var group in Model.GroupBy(item => item.SKU))
                {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => group.Key)
                        </td>
                   </tr>
                    foreach (var item in group.OrderBy(o => o.Id))
                    {
                        <tr>
                            <td></td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Qty)
                            </td>
                            .........
                            .........
                            .........

我怎么能做到这一点?

4

2 回答 2

0

I am not sure if I understood your requirement completely however I think you are trying to perform some aggregates on the data you have received on the client side. If that is the case you can use

Linqjs

Its pretty awesome and gives you the ability to use .Net methods at client side code.

Another way is, define a ViewModel and add QtySum and LineCount property. Compute that at server side code and return the ViewModel to client. Client side code will use these values to render UI.

Update 1:

Looking at more information on what you want to achieve I am sure you definitely want to go with defining ViewModel approach. I prefer that as it give me more control as I am writing server side code and has all the abilities (.net methods). Second I prefer to keep view as dumb and lightweight as possible.

In you current scenario, I would define view model as below (consider this example for guidance and modify as per your need):

public class PickingListViewModel
{
    public int Id { get; set; }
    public int TransID { get; set; }
    public PickingListSkuViewModel SkuGroupModel { get; set; }
}

public class PickingListSkuViewModel
{
    public string SKU { get; set; }
    public IEnumerable<PickingListItemViewModel> GroupItems { get; set; }
}

public class PickingListItemViewModel
{
    public int ColorId { get; set; }
    public string Color { get; set; }
    public double Qty { get; set; }
    public decimal Price { get; set; }
}

Now at controller I can fill the view model like so (again note that preferred approach is to fetch the data from business or data layer):

public ActionResult Index()
{
    ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

    // Read this data from business layer
    var group1 = new PickingListSkuViewModel
    {
        SKU = "61009",
        GroupItems = new List<PickingListItemViewModel>
        {
            new PickingListItemViewModel
            {
                Color = "Blue",
                Qty = 12,
                Price = (decimal) 2.5
            },
            new PickingListItemViewModel
            {
                Color = "Red",
                Qty = 3,
                Price = (decimal) 4.5
            }
            ,
            new PickingListItemViewModel
            {
                Color = "Green",
                Qty = 4,
                Price = (decimal) 1.5
            }
        }
    };

    var group2 = new PickingListSkuViewModel
    {
        SKU = "61011",
        GroupItems = new List<PickingListItemViewModel>
        {
            new PickingListItemViewModel
            {
                Color = "Blue",
                Qty = 12,
                Price = (decimal) 2.5
            },
            new PickingListItemViewModel
            {
                Color = "Red",
                Qty = 3,
                Price = (decimal) 4.5
            }
            ,
            new PickingListItemViewModel
            {
                Color = "Green",
                Qty = 4,
                Price = (decimal) 1.5
            }
        }
    };

    var model = new List<PickingListViewModel>
    {
        new PickingListViewModel
        {
            Id = 1,
            TransID = 101,
            SkuGroupModel = group1
        },
        new PickingListViewModel
        {
            Id = 2,
            TransID = 102,
            SkuGroupModel = group2
        }
    };

    return View(model);
}

Controller should just initialize the model and return it to appropriate view.

I have not included view code as I believe you are comfortable with it. As hint you now have all the information per group, just write rendering code. You can add sum or count or any other aggregate into PickingListItemViewModel or simple computation can be performed on view code e.g. TotalPrice = Quantity * Price

Let me know if you have any follow up question. Hope this helps.

于 2014-04-28T05:39:07.043 回答
0

正如 SBirthare 建议的那样,我认为您可能应该创建一个视图模型并将其中的一些工作移到那里。但是,要回答您的问题,您可能正在寻找以下内容:

<tr>
  <td>
    @Html.DisplayFor(modelItem => group.Key)
  </td>
  <td>
    @Html.DisplayFor(modelItem => group.Sum(item => item.Qty))
    <span> pieces</span>
  </td>
</tr>

如果我误解了你的问题,请告诉我。

于 2014-04-29T18:56:39.047 回答