Access global page variable in helper

后端 未结 3 1871
执念已碎
执念已碎 2021-02-06 20:47
@{
    int i = 0;
}

@helper Text() {
    
}

i is not accessible in helper. How to acce

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-06 21:14

    You could pass it as parameter to the helper:

    @helper Text(int i) {
        
    }
    

    and then:

    @{
        int i = 0;
    }
    @SomeHelper.Text(i)
    

    or you could simply use editor templates which will take care of everything and get rid of those helpers. For example:

    @Html.EditorFor(x => x.Ans)
    

提交回复
热议问题