ASP.NET MVC3(Razor)中的视图中局部变量的简单增量

前端之家收集整理的这篇文章主要介绍了ASP.NET MVC3(Razor)中的视图中局部变量的简单增量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
那么这只是尴尬。我甚至不能在ASP.NET MVC3(Razor)中看到一个简单的增量。我已经做了搜索,看来Razor的文档是相当稀疏的。这是我尝试过的并且失败的惨败:
@{
    var counter = 1;

    foreach (var item in Model.Stuff) {
        ... some code ...
        @{counter = counter + 1;}
    }
}

我也试过@ {counter;}只是为了踢,无济于事)如果有人能启发我,我会感激的。谢谢!

解决方法

@{
    int counter = 1;

    foreach (var item in Model.Stuff) {
        ... some code ...
        counter = counter + 1;
    }
}

说明:

@{
// csharp code block
// everything in here is code,don't have to use @
int counter = 1;
}

@foreach(var item in collection){
    <div> - **EDIT** - html tag is necessary for razor to stop parsing c#
    razor automaticaly recognize this as html <br/>
    this is rendered for each element in collection <br/>
    value of property: @item.Property <br/>
    value of counter: @counter++
    </div>
}
this is outside foreach
原文链接:https://www.f2er.com/aspnet/253906.html

猜你在找的asp.Net相关文章