MVC Razor @foreach

后端 未结 5 1720
半阙折子戏
半阙折子戏 2020-11-27 13:13

I heard that having @foreach inside of a view is a no-no. Meaning, the view should not have any logic in it. What is the best practice on where the logic for the @foreach sh

5条回答
  •  清酒与你
    2020-11-27 13:34

    I'm using @foreach when I send an entity that contains a list of entities ( for example to display 2 grids in 1 view )

    For example if I'm sending as model the entity Foo that contains Foo1(List) and Foo2(List)

    I can refer to the first List with:

    @foreach (var item in Model.Foo.Foo1)
    {
        @Html.DisplayFor(modelItem=> item.fooName)
    }
    

提交回复
热议问题