C# 要查看的IEnumerable MVC控制器

C# 要查看的IEnumerable MVC控制器,c#,asp.net-mvc,linq,web,ienumerable,C#,Asp.net Mvc,Linq,Web,Ienumerable,我试图从控制器传递一个var,并访问视图中的信息 在控制器中,我有下面的LINQ语句,它汇总了我需要的一些列的总和。在过去,我只是将var传递给一个列表,然后将该列表传递给其他人 问题是我不确定如何传递这个var 以下是控制器代码 var GoodProduct = new { CastGoodSum = (from item in db.tbl_dppITHr where item.ProductionHour >= StartShift

我试图从控制器传递一个var,并访问视图中的信息

在控制器中,我有下面的LINQ语句,它汇总了我需要的一些列的总和。在过去,我只是将var传递给一个列表,然后将该列表传递给其他人

问题是我不确定如何传递这个var

以下是控制器代码

    var GoodProduct =
new
{
    CastGoodSum =
        (from item in db.tbl_dppITHr
         where item.ProductionHour >= StartShift && item.ProductionHour <= EndDate
         select item).Sum(x => x.CastGood),

    CastScrap =
        (from item in db.tbl_dppITHr
         where item.ProductionHour >= StartShift && item.ProductionHour <= EndDate
         select item).Sum(x => x.Scrap),

    MachinedSum = 
    (
    from item in db.tbl_dppITHr
    where item.ProductionHour >= StartShift && item.ProductionHour <= EndDate
    select item).Sum(x => x.Machined),
};

        return View(GoodProduct);
var GoodProduct=
新的
{
卡斯特古德森姆酒店=
(来自db.tbl\u dppITHr中的项目
其中item.ProductionHour>=开始换档和&item.ProductionHour x.CastGood),
铸造废料=
(来自db.tbl\u dppITHr中的项目
其中item.ProductionHour>=开始换档和&item.ProductionHour x.Scrap),
机械总和=
(
从db.tbl\U dppITHr中的项目
其中item.ProductionHour>=开始换档和item.ProductionHour x,
};
返回视图(GoodProduct);
我使用的视图是强类型i,具有以下IEnmerable

@model IEnumerable<int?>
@model IEnumerable
我也试过了

@model IEnumerable<MvcApplication1.Models.tbl_dppITHr>
@model IEnumerable
当我传递一个单一值类型时,这很好,但是因为我在做一个求和,所以我得到了以下错误

The model item passed into the dictionary is of type '<>f__AnonymousType2`3[System.Nullable`1[System.Int32],System.Nullable`1[System.Int32],System.Nullable`1[System.Int32]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[System.Nullable`1[System.Int32]]'.
传递到字典的模型项的类型为'f_uAnonymousType2`3[System.Nullable`1[System.Int32],System.Nullable`1[System.Int32],System.Nullable`1[System.Int32]],但此字典需要'System.Collections.Generic.IEnumerable`1[System.Nullable`1[System.Int32]]类型的模型项。

有人知道如何传递此变量吗?

现在,您需要使用:

@model dynamic
创建动态对象时,将其传递给视图

但是,我更喜欢创建强类型视图模型并将其传递给视图。 i、 e

然后将其填充到控制器中

var GoodProduct = new GoodProductViewModel
{
    CastGoodSum = ....,
    CastScrap = ...,
    MachinedSum = ...
};

return View(GoodProductViewModel);

并在视图中使用
@model GoodProductViewModel

现在,您需要使用:

@model dynamic
创建动态对象时,将其传递给视图

但是,我更喜欢创建强类型视图模型并将其传递给视图。 i、 e

然后将其填充到控制器中

var GoodProduct = new GoodProductViewModel
{
    CastGoodSum = ....,
    CastScrap = ...,
    MachinedSum = ...
};

return View(GoodProductViewModel);

在视图中使用
@model GoodProductViewModel

我想GoodProduct不是一个数组,而是一个对象。你能精确计算他的类型吗?GoodProduct似乎是一个匿名对象,包含3个可为空的整数。如果GoodProduct是一个强类型类的实例,那么这个类可以用作视图的模型我想GoodProduct不是一个数组,而是一个对象。你能精确计算他的类型吗?GoodProduct似乎是一个匿名对象,包含3个可为空的整数。如果GoodProduct是一个强类型类的实例,那么这个类可以用作视图的模型。你的回答速度比我快:)注意,你不需要使用
@model dynamic
,完全省略
@model
具有相同的效果,如果我使用dynamic,我将如何从视图中访问var中的项?如果它是强类型模型,例如model.castgoodsum有一点问题,您也会这样做。当运行代码my VAR GoodProduct={CastGood=690,goodscrapt=0}时,在我的视图中,我有'@Model.CastGood,我得到的错误是@Model不包含CastGood的定义。如何在GoodProduct中访问这些项您在回答速度上胜过了我:)注意,您不需要使用
@model dynamic
,完全省略
@model
具有相同的效果如果我使用dynamic,我如何从视图中访问var中的项?如果它是强类型模型,您将以相同的方式,模型有点问题。当运行代码my VAR GoodProduct={CastGood=690,goodscrapt=0}时,在我的视图中,我有'@Model.CastGood,我得到的错误是@Model不包含CastGood的定义。如何访问GoodProduct中的这些项目