0

Below is my razor code

for (int i = 0; i < Model.physicians.Count; i++)
{ 
    <div>
        @Model.physicians[i].npi -- Count: 
    </div>
}

I have table npipatients as below:

npiId Patientname
1 a
1 b
2 c

My output like this in razor view

1 -- Count:2

2 -- Count:1

I need to write ajax call but how to pass npiid to ajax method for foreach and assign result of npi count to label.

$.ajax({
    type: "POST",
    url: "/mycontroler/GetnpiDetails",
    dataType: "json",
    data: { "npiId": npiId },                    
    success: function (data) {


   }
});
4
  • 1
    my general advise although not answering your question directly would be to shape the data in a way your view needs it first -instead of wrangling it after the fact Commented Feb 7, 2022 at 7:50
  • Why not just doing .GroupBy() in the controller first and return the grouped result to View. Commented Feb 7, 2022 at 7:51
  • i did not get you. @Jazb Commented Feb 7, 2022 at 7:52
  • can you post sample code for understanding . @YongShun Commented Feb 7, 2022 at 7:54

1 Answer 1

1

From what I understand based on your question (Please correct me if I'm wrong), looks like you are trying to fire the API with each Npi record for getting the (grouping) count.

In my perspective, it is NOT EFFICIENT, imagine there are 100 records and you will call the API 100 times. This will lead to pulling down your server performance (Handling the same task 100 times) and also database server (Query the same data 100 times).

Instead,

You need to do the data transformation in Controller first by .GroupBy() the npi record and .Count() for each grouped record.

Expected output:

[
  { "npi": 1, "count": 2 },
  { "npi": 2, "count": 1 }
]

With 1 single API call saves you the performance from calling m time as discussed.

What's you need to do:

  1. Model - Design your desired output with the model class.
  2. Controller - Perform data transformation: Grouping by npiId.
  3. View - Bind the value from Controller.

Model

public class GroupedNpiModel
{
    public int NpiId { get; set; }

    public int Count { get; set; }
}

Controller

public ActionResult Index()
{
    var data = new []
    {
        new { NpiId = 1, Patientname = "a" } ,
        new { NpiId = 1, Patientname = "b" } ,
        new { NpiId = 2, Patientname = "c" } 
    };
            
    var groupedNpi = data.GroupBy(x => x.NpiId)
        .Select(g => new GroupedNpiModel
        {
            NpiId = g.Key,
            Count = g.Count()
        })
        .ToList();
        
    return View(groupedNpi);
}

View

@model IEnumerable<HelloWorldMvcApp.GroupedNpiModel>

@foreach (var npi in Model)
{
    <div>
        @npi.NpiId -- @npi.Count
    </div>
}

Sample program on .NET Fiddle

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

Comments

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.