在执行下面的代码时,我在控制器中收到此错误:
无法隐式转换类型“int”?到“int”。存在显式转换(您是否缺少强制转换?)
出现错误t.TOT_QTY
在我的控制器中。
控制器:
dcms = (from t in db.ASN_ITEM
...
where
(t.TOT_QTY - t.SCND_QTY) != 0
select new ASNVarianceRep
{
TOT_QTY = t.TOT_QTY,
SCND_QTY = t.SCND_QTY,
VAR_QTY = (t.SCND_QTY - t.TOT_QTY)
}).Distinct();
Where t.TOT_QTY
属于类型int?
在数据库中。但变量TOT_QTY
只是一个int
在模型类上。
Model:
public class ASNVarianceRep : ASN_ITEM
{
public int TOT_QTY { get; set; }
...
}
如果我改变了public int TOT_QTY { get; set; }
to public Nullable<int> TOT_QTY { get; set; }
,那么控制器中的错误将得到解决。但在查看页面的行上也会出现同样的错误itotqty = itotqty + item.TOT_QTY;
View:
@if (Model != null)
{
var itotqty = 0;
foreach (var item in Model)
{
itotqty = itotqty + item.TOT_QTY;
<tr>
<td>
@Html.DisplayFor(modelItem => item.TOT_QTY)
</td>
...
</tr>
}
<tr>
<td style="text-align: center"><b>@itotqty.ToString() </b></td>
</tr>
所以,我想要的是我需要制作以下类型t.TOT_QTY
from int?
To int
在控制器或制作var itotqty = 0;
from int
to int?
在查看页面上。
怎么办呢?
将您的更改保留在模型中,然后保留在您的视图中
totqty = itotqty + Convert.ToInt32(item.TOT_QTY);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)