MVC传递数据的四件到控制器,并随后查看 [英] MVC Passing Four Pieces of Data to the Controller and subsequently to the View

查看:92
本文介绍了MVC传递数据的四件到控制器,并随后查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是我的其他问题的B部分在这里:

This question is part B of my other question here:

<一个href=\"http://stackoverflow.com/questions/17318954/inefficient-mvc-viewmodel-making-multiple-calls-to-the-database\">Inefficient MVC视图模型进行多次调用数据库?

我知道多次调用该数据库是一个坏主意。但是,我想我真正的问题是,如果我用我的code或speti43的榜样最后一块比方说像这样在我的模型构造,以消除多个数据库调用:

I knew multiple calls to the db was a bad idea. But, I guess my real question is if I use my last chunk of code or speti43's example let's say like this in my model constructor to eliminate multiple db calls:

    Public Class OrdersSummary{
    ...
      Public ???? GetOrders(){

        var ordersInMemory = orders.ToList();
        decimal? GrossProfitTotal = ordersInMemory.Sum(s => s.Profit);
        decimal? CommissionTotal = ordersInMemory.Sum(s => s.Commission);
        decimal? NetProfitTotal = GrossProfitTotal + CommissionTotal;
        return ?????

      }
    }

我如何通过这些独立的,但类似的数据块到控制器?我试图用这样的元组基于其他一些SO文章推荐位置:

How do I pass those seperate but similar pieces of data to the controller? I was trying to use a tuple like this based on some other SO article recommendation here:

<一个href=\"http://stackoverflow.com/questions/3845831/how-to-return-multiple-values-from-a-function-in-c-sharp-asp-net\">How从一个函数在C#中返回多个值(ASP.NET)?

public Tuple<IEnumerable<dynamic>, decimal?, decimal?, decimal?> GetOrders(string sortOrder, string searchString)
{
...

    return new Tuple<IEnumerable<dynamic>,decimal?,decimal?,decimal?> (ordersInMemory, GrossProfitTotal, CommissionTotal, NetProfitTotal);
}

这听起来合理吗?然后,我不知道如何引用元组在我的控制器:

Does this sound reasonable? Then I have no idea how to reference the tuple in my controller:

        var ordersummary = new OrdersSummary();
        var viewModel = new OrderSummary
        {
            ???? cause I have no idea how to reference the tuple
        };
        return View(viewModel);

另一个问题是我不能完全肯定我建设,我认为正确的模型的属性。目前我有像这样的四部分数据和订单列表,那么各个属性:

The other issue is I'm not entirely sure I'm building the properties correct in my view model. Currently I have it like this for the four pieces of data and then individual properties of the Orders list:

public class OrderSummary
    {
        public IEnumerable<dynamic> Orders { get; set; }
        public decimal GrossProfitTotal { get; set; }
        public decimal CommissionTotal { get; set; }
        public decimal NetProfitTotal { get; set; }

        //Orders Table Properties
        public int OrderNumber { get; set; }
        public DateTime OpenTime { get; set; }
        //more properties here that correspond to the Orders table.
    }

好像我可能需要在结构视图模型不同的元组属性?如果是这样,有什么语法?

It seems like I might need to structure the tuple properties different in the view model? If so, what syntax?

我想我可能知道在视图发生的事情,如果我知道在控制器中发生的事情。这是甚至去了解它的正确方法?我从上MVCMUsicStore教程步骤8,这个整体视图模型结构的想法:

I think I might know what goes in the View if I knew what goes in the controller. Is this even the correct way to go about it? I got this whole view model structure idea from step 8 on the MVCMUsicStore tutorial:

HTTP:// WWW。 asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-8

推荐答案

很多咒骂,更是红色的波浪线之后,我想通了一对夫妇方法可以做到这一点,并没有涉及到一个元组:

After much cursing and even more red squiggly lines, I figured out a couple ways to do this and neither involves a tuple:

第一种方法:

视图模型:

public IEnumerable<OrderSummary> Orders { get; set; }
public decimal? GrossProfitTotal { get { return Orders.Sum(s => (decimal)s.Profit); } }
public decimal? CommissionTotal { get { return Orders.Sum(s => (decimal)s.Commission); } }
public decimal? NetProfitTotal { get { return GrossProfitTotal + CommissionTotal; } }

控制器:

var orderssummary = new OrdersSummary();
var viewModel = new OrderSummary
{
   Orders = orderssummary.GetOrders(sortOrder, searchString),
}

return View(viewModel);

第二种方法:

视图模型:

public IEnumerable<OrderSummary> Orders { get; set; }        
public decimal? GrossProfitTotal { get; set; }
public decimal? CommissionTotal { get; set; }
public decimal? NetProfitTotal { get; set; }

控制器:

    var orderssummary = new OrdersSummary();
    var orderslist = orderssummary.GetOrders(sortOrder, searchString);

    decimal? grossprofittotal = orderslist.Sum(s => (decimal)s.Profit);
    decimal? commissiontotal = orderslist.Sum(s => (decimal)s.Commission);
    decimal? netprofittotal = orderslist.Sum(s => (decimal)s.Profit + s.Commission);

        var viewModel = new OrderSummary
        {
            Orders = orderslist,
            GrossProfitTotal = grossprofittotal,
            CommissionTotal = commissiontotal,
            NetProfitTotal = netprofittotal,
        };

    return View(viewModel);

查看(对于第一和第二种方法):

@foreach (var item in Model.Orders){
   @item.OrderNumber
   @item.OpenTime
   etc.
}
    @Model.CommissionTotal
    @Model.GrossProfitTotal
    @Model.NetProfitTotal

我目前使用的1号,因为它似乎干净了一点。我相信现在通过我的IEnumerable循环为其他值而不是打为每一个数据库?我很想弄清楚如何计算在一个循环中的所有值。

I am currently using number 1 as it appears a little cleaner. I believe it is now looping through my Ienumerable for the other values instead of hitting the db for each one? I would love to figure out how to calculate all values on a single loop.

这似乎也我甚至不真的需要数1视图模型,但我会添加其他部分信息的像图表数据的看法,所以我会离开我的选择余地。

It also appears I don't even really need a viewmodel for number 1, but I'll be adding other pieces of info to the view like chart data, so I will leave my options open.

我总是想知道别人的想法,如果他们有一个preference为一个或一个整体更好的办法。

I'm always interested to know people's thoughts and if they have a preference for either or a whole better way.

这篇关于MVC传递数据的四件到控制器,并随后查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆