Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 262
user2810643 Avatar asked Dec 15 '22 01:12

user2810643


2 Answers

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)
    {
        ...
    }
}
like image 137
iappwebdev Avatar answered Jan 23 '23 06:01

iappwebdev


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

@((YourType) row)
like image 42
Robin Dorbell Avatar answered Jan 23 '23 04:01

Robin Dorbell