指定的强制转换是无效的ASP.NET MVC4

时间:2013-11-06 12:25:29

标签: asp.net-mvc linq razor

我有LINQ查询从实体框架连接获取一些数据。我将数据从控制器传递给View。运行代码时,我收到错误“指定的强制转换无效”。

这是我的LINQ声明

var MeltAreaInformation =
    new
    {
        Striko1 = (from item in db.tbl_dppITHr where item.ProductionHour >= SelectedDateDayShiftStart && item.ProductionHour <= SelectedDateDayShiftHr25End select item).Sum(x => x.Striko1) ?? 0,

              Striko2 =
             (from item in db.tbl_dppITHr
              where item.ProductionHour >= SelectedDateDayShiftStart && item.ProductionHour <= SelectedDateDayShiftHr25End
              select item).Sum(x => x.Striko2) ?? 0,

              Striko3 =
             (from item in db.tbl_dppITHr
              where item.ProductionHour >= SelectedDateDayShiftStart && item.ProductionHour <= SelectedDateDayShiftHr25End
              select item).Sum(x => x.Striko3) ?? 0,

              Striko4 =
             (from item in db.tbl_dppITHr
              where item.ProductionHour >= SelectedDateDayShiftStart && item.ProductionHour <= SelectedDateDayShiftHr25End
              select item).Sum(x => x.Striko4) ?? 0,

              Striko5 =
             (from item in db.tbl_dppITHr
              where item.ProductionHour >= SelectedDateDayShiftStart && item.ProductionHour <= SelectedDateDayShiftHr25End
              select item).Sum(x => x.Striko5) ?? 0,

              Induction1 =
             (from item in db.tbl_dppITHr
              where item.ProductionHour >= SelectedDateDayShiftStart && item.ProductionHour <= SelectedDateDayShiftHr25End
              select item).Sum(x => x.Inductotherm1) ?? 0,

              Induction2 =
             (from item in db.tbl_dppITHr
              where item.ProductionHour >= SelectedDateDayShiftStart && item.ProductionHour <= SelectedDateDayShiftHr25End
              select item).Sum(x => x.Inductotherm2) ?? 0,
    };


        ViewData["Striko2"] = MeltAreaInformation.Striko1.ToString();

现在当我在调试中运行应用程序并将鼠标悬停在Var MeltAreaInformation上时,您可以看到它已为其分配了以下内容。 enter image description here

以下是我用于在HTML页面上显示ViewData的Razor语法。

<table class="MeltTable">
<tr><th colspan="7">Total Weight Poured (kg's)</th></tr>
<tr><th>Striko 2</th><td class="MeltTableZero td @((int)ViewData["Striko2"] == 0 ? "red" : null)">@ViewData["Striko2"].ToString()</td></tr>
<tr><th>Striko 3</th><td class="MeltTableZero td @((int)ViewData["Striko3"] == 0 ? "red" : null)">@ViewData["Striko3"].ToString()</td></tr>
<tr><th>Striko 4</th><td class="MeltTableZero td @((int)ViewData["Striko4"] == 0 ? "red" : null)">@ViewData["Striko4"].ToString()</td></tr>
<tr><th>Striko 1</th><td class="MeltTableZero td @((int)ViewData["Striko1"] == 0 ? "red" : null)">@ViewData["Striko1"].ToString()</td></tr>
<tr><th>Striko 5</th><td class="MeltTableZero td @((int)ViewData["Striko5"] == 0 ? "red" : null)">@ViewData["Striko5"].ToString()</td></tr>
<tr><th>Induction 1</th><td class="MeltTableZero td @((int)ViewData["Inductotherm1"] == 0 ? "red" : null)">@ViewData["Inductotherm1"].ToString()</td></tr>
<tr><th>Induction 2</th><td class="MeltTableZero td @((int)ViewData["Inductotherm2"] == 0 ? "red" : null)">@ViewData["Inductotherm2"].ToString()</td></tr>
</table>

任何人都可以对这个问题有所了解。我试图手动分配值,但我仍然遇到同样的问题。

1 个答案:

答案 0 :(得分:1)

ViewData["Striko2"] = MeltAreaInformation.Striko1.ToString();

这是您的问题 - 您将ViewData包中的值作为字符串放置,然后尝试在您的视图中投射它们:

@((int)ViewData["Striko2"] == 0 ? "red" : null)

这不会发生!要么将值包装为其类型(int),要么不在视图中投射它们(您可以在没有投射的情况下编写字符串) - 您的调用