Simple increment of a local variable in views in ASP.NET MVC3 (Razor)

后端 未结 5 913
借酒劲吻你
借酒劲吻你 2020-12-09 07:37

Well, this is just embarrassing. I can\'t even figure out a simple increment in one of my views in ASP.NET MVC3 (Razor). I have done searches and it appears that the docum

5条回答
  •  我在风中等你
    2020-12-09 07:58

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

    Explanation:

    @{
    // csharp code block
    // everything in here is code, don't have to use @
    int counter = 1;
    }
    
    @foreach(var item in collection){
        
    - **EDIT** - html tag is necessary for razor to stop parsing c# razor automaticaly recognize this as html
    this is rendered for each element in collection
    value of property: @item.Property
    value of counter: @counter++
    } this is outside foreach

提交回复
热议问题