Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

83
Views
I want to display one by one value in foreah loop according to demo Image

enter image description here

In a model with 180 records, I want to display one at a time. when I click on the next button the next record is shown with a refreshed page.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can try to use TempData and partial view,here is a demo:

Model:

public class LoopModel{
       public List<string> l { get; set; }
        public int count { get; set; }
    }

Action:

[HttpGet]
        public IActionResult TestLoop()
        {
            
            LoopModel loopModel = new LoopModel { l = new List<string> { "s1", "s2", "s3", "s4", "s5" }, count = 0 };
           TempData["model"] =JsonConvert.SerializeObject(loopModel);
            return View();
        }
        [HttpPost]
        public IActionResult TestLoop(int count)
        {
            var s = TempData.Peek("model").ToString();
            LoopModel loopModel = JsonConvert.DeserializeObject<LoopModel>(TempData.Peek("model").ToString());
            if(count<loopModel.l.Count())
            loopModel.count = count;
            return PartialView("Partial1",loopModel);
        }

TestLoop.cshtml:

@using Newtonsoft.Json;
<div id="myDiv">
    @await Html.PartialAsync("Partial1", JsonConvert.DeserializeObject<LoopModel>(TempData.Peek("model").ToString()))
</div>
<button onclick="AddContent()">next</button>
@section Scripts
{
    <script>
        var count = 0;


        function AddContent() {
            count++;
            $.ajax({
                type: "POST",
                url: '?count=' + count,
                success: function (result) {
                    $("#myDiv").html(result);
                }
            });
    }
    </script>
}

Partial1:

@model LoopModel
@for (var i = 0; i <= Model.count; i++)
{
    <div class="container">@Model.l[i]</div>
}

result: enter image description here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!