1

I am rendering HTML with a foreach loop, like below :

@foreach (var item in list)
 {
     // some html
 }

But it seems that it is iterating the whole loop before starting to render the HTML. I would like to display (render) the html one by one (i.e. if loop iterates 100 times, then it should be render/display the HTML one by one to 100).

Note : To verify that it starts to render the HTML when the loop ends, I did put a thread.sleep(100) inside the loop

@foreach (var item in list)
     {
         Thread.Sleep(100);
         // some html
     }

and if it takes 10 mseconds to start rendering the HTML without sleep then after putting thread.sleep it takes 1000 mseconds before starting to render HTML.

Any suggestion as to what I should do so that the UI doesn't get frozen until the loop completes? It should render the HTML as each iteration completes. Thank you.

1 Answer 1

1

If you are doing this inside of a Razor view, then all your HTML will be rendered on the server BEFORE it is sent down to the client.

If you want to render something one by one, then you will have to do that on the client via JavaScript.

Sign up to request clarification or add additional context in comments.

2 Comments

i noticed on most of the sites the content starts render when a page opens and it keeps loading and further rendering the records/content, What about those ?
Is there no server side scripting or any way that it could render records one by one ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.