当前位置 : 主页 > 网络编程 > ASP >

asp.net-mvc – 如何使用razor语法转换为对象

来源:互联网 收集:自由互联 发布时间:2021-06-24
@using System.Data@model DataTable@foreach (var row in Model.Rows){ @row[] // how do you cast this to a object?} 如何使用Razor语法将@row转换为对象? 你可以编写常见的C#代码: @foreach (YourType row in Model.Rows){ ...
@using System.Data
@model DataTable

@foreach (var row in Model.Rows)
{
   @row[]  // how do you cast this to a object?
}

如何使用Razor语法将@row转换为对象?

你可以编写常见的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)
    {
        ...
    }
}
网友评论