user2810643
user2810643

Reputation: 61

how to cast to object in razor syntax

@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

Answers (2)

Robin Dorbell
Robin Dorbell

Reputation: 1619

I happened upon this problem today. The solution I used was using parenthesis:

@((YourType) row)

Upvotes: 6

iappwebdev
iappwebdev

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

Related Questions