【问题标题】:Specified cast is not valid ASP.NET MVC4指定的强制转换无效 ASP.NET MVC4
【发布时间】:2013-11-06 20:45:30
【问题描述】:

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

这是我的 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 的调试和悬停中运行应用程序时,您可以看到它分配了以下内容。

以下是我用来在 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>

谁能解释一下这个问题。我尝试手动分配值,但仍然遇到同样的问题。

【问题讨论】:

  • 所有的返回值都是Int32的吗?
  • 你能显示所有的 ViewData 分配吗?
  • 等一下 - 你正在 ToString()ing ViewData 中的值 - 你不能将字符串转换为 int...

标签: asp.net-mvc linq razor


【解决方案1】:

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

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

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

这不会发生!要么将值作为它们的类型 (int) 打包,要么不将它们投射到视图中(你可以只编写没有投射的字符串) - 你的电话

【讨论】:

  • 我在代码的不同部分也遇到了同样的问题,其中 Razor 是 @ViewData["PumaShipped"] 并且它的值也不是空值
  • 你能在调试器中断的视图中发布行(和周围的行)以及相关的控制器/视图模型代码吗?
  • 它在 @ViewData["DVEuro5Shipped"] 控制器代码 var DVEuro5Product = new{ DVEuro5Shipped = (from item in db.tbl_dppITHr where item.ProductionHour >= SelectedDateDayShiftStart && item.ProductionHour x.DVE5Shipped) ?? 0}
  • 您能否将代码发布在原始问题(格式化)中,因为它更易于阅读,并发布您将项目放入ViewData 袋子(ViewData["xxx"] = value)的部分,同样多尽可能 - 我猜我会说你在那里删除了一个匿名类型,并且这些值没有成为 ViewData 包中的键。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-27
  • 2014-07-30
  • 2012-06-12
相关资源
最近更新 更多