@using System.Data
@model DataTable
@foreach (var row in Model.Rows)
{
@row[] // how do you cast this to a object?
}
如何使用Razor语法将@row转换为对象?
答案 0 :(得分:7)
您可以编写常见的C#代码:
@foreach (YourType row in Model.Rows)
{
...
}
或
@foreach (var row in Model.Rows)
{
YourType casted = (YourType)row;
...
}
或者如果你不确定它是否可以施放:
@foreach (var row in Model.Rows)
{
YourType casted = row as YourType;
if (casted != null)
{
...
}
}
答案 1 :(得分:0)
我今天遇到了这个问题。我使用的解决方案是使用括号:
@((YourType) row)