Reputation: 61
@using System.Data
@model DataTable
@foreach (var row in Model.Rows)
{
@row[] // how do you cast this to a object?
}
How do you cast @row to an object in Razor syntax?
Upvotes: 4
Views: 6200
Reputation: 1619
I happened upon this problem today. The solution I used was using parenthesis:
@((YourType) row)
Upvotes: 6
Reputation: 5910
You can just write common C# code:
@foreach (YourType row in Model.Rows)
{
...
}
or
@foreach (var row in Model.Rows)
{
YourType casted = (YourType)row;
...
}
or if you're not sure if it's castable:
@foreach (var row in Model.Rows)
{
YourType casted = row as YourType;
if (casted != null)
{
...
}
}
Upvotes: 8