Search code examples
razorrazor-components

Razor component instances created in loop - parameters problem


I would like generate instances of component like these:

            for (int i = 0; i < 10; i++)
            {
                <MyComponent Index="@i">
                    @i
                </MyComponent>
            }

Problem is that every component instance has in final view the same value of param Index, and this value is 9. It si probably because this is right variables value when render time. Is it possible to pass right value of "i" variable to components? Thanks very much.


Solution

  • Answer is very simple :-D

            for (int i = 0; i < 10; i++)
            {
                int @iHlp = @i;
                <MyComponent Index="@iHlp">
                    @iHlp
                </MyComponent>
            }