0

ASP.NET MVC application, so I started a new program that contains the count of items in the database based on the group, and I have used the ASP.NET MVC to do it.

My model class have the following details

public class Project
{
    public int ProjectID { get; set; }
    [Required]
    public string ProjectName { get; set; }
    [Required]
    public string ProjectDescription { get; set; }
    [Required]
    public string ProjectScope { get; set; }
    [Required]
    public string ProjectFamily { get; set; }
    [Required]
    public string ProjectBucket { get; set; }
    [Required]
    public string ProjectType { get; set; }
    [Required]
    public string ProjectStatus { get; set; }

    public string Categoryname { get; set; }
    public int Count { get; set; }
}

and Controller has the following code

public IActionResult Count()
    {
        //Your EntityModel or business logic for fetching the records.
        ProjectDataAcces entities = new ProjectDataAcces();
        var images = entities.GetAllProject().GroupBy(n => n.ProjectFamily)
                            .Select(g => new { ProjectFamily = g.Key, Count = g.Count() }).ToList();
        List<Project> imagesList = new List<Project>();
        for (int i = 0; i < images.ToList().Count; i++)
        {
            imagesList.Add(new Project { ProjectFamily = images[i].ProjectFamily, Count = images[i].Count });
        }
        return View(imagesList);

    }

by using this I have to get the count in my view page. How can I do this?

and my view page is

                                <div class="col-6 col-md-4 col-xl-2">
                                    <div class="card ca-bg">
                                        <div class="card-body ribbon bucket-cursor">
                                            <div class="ribbon-box orange" id="count1"></div>
                                            <div class="my_sort_cut text-muted1">
                                                <i class="icon-list"></i>
                                                <span>800xA Incl Safety</span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-6 col-md-4 col-xl-2">
                                    <div class="card ca-bg">
                                        <div class="card-body ribbon  bucket-cursor">
                                            <div class="ribbon-box orange" id="count2"></div>
                                            <div class="my_sort_cut text-muted1">
                                                <i class="icon-like"></i>
                                                <span>Aprol</span>
                                            </div>

                                        </div>
                                    </div>
                                </div>
                                <div class="col-6 col-md-4 col-xl-2">
                                    <div class="card ca-bg">
                                        <div class="card-body ribbon bucket-cursor">
                                            <div class="ribbon-box orange" id="count3"></div>
                                            <div class="my_sort_cut  text-muted1 ">
                                                <i class="icon-credit-card"></i>
                                                <span>Compact Products</span>
                                            </div>

                                        </div>
                                    </div>
                                </div>
                                <div class="col-6 col-md-4 col-xl-2">
                                    <div class="card ca-bg">
                                        <div class="card-body ribbon bucket-cursor">
                                            <div class="ribbon-box orange" id="count4"></div>
                                            <div class="my_sort_cut  text-muted1 ">
                                                <i class="icon-doc"></i>
                                                <span>Freelance</span>
                                            </div>
                                        </div>
                                    </div>
                                </div>

In this I have to show the count in id="count1" id="count2" place....How to view the count by group by?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sumo15
  • 33
  • 5

2 Answers2

1

You can use a foreach loop to achieve your functionality. A foreach loop will iterate over your List and display each element in the list. Specifically to your case, you can do:

  @foreach(var image in Model.imagesList)
  {
    <div class="col-6 col-md-4 col-xl-2">
      <div class="card ca-bg">
        <div class="card-body ribbon bucket-cursor">
            <div class="ribbon-box orange" id="@image.Count"></div>
                <div class="my_sort_cut text-muted1">
                    <i class="icon-list"></i>
                    <span>@image.ProjectFamily</span>
                </div>
            </div>
        </div>
     </div>
  }
Rahul Sharma
  • 7,768
  • 2
  • 28
  • 54
  • This one is fine , but if am using the controller section code (ie) IActionResult Count i can't able to get the output instead of IActionResult Count I have using IActionResult Index then the output will be okey – Sumo15 Dec 17 '19 at 08:56
  • Well, basically you would need to assign the `Count` to the corresponding group which you are doing and then you need to send that `Model` to your corresponding `View`. In your case, your `Count` method will look for a view called `Count` so you can do this: `return View("Index",imagesList);` – Rahul Sharma Dec 17 '19 at 08:58
  • @PakkiyaNathan You need to check what values are you getting in your `imagesList` specifically for `Count = images[i].Count` – Rahul Sharma Dec 17 '19 at 10:58
  • am using one model called **Project** in that i have declared the Count and In my Controller file am using the **IActionResult Count** .......Instead of Count using index then it shows the correct output Why – Sumo15 Dec 17 '19 at 11:19
  • @PakkiyaNathan You are getting confused it seems, your `IActionResult Count` is not like a normal method. You would expect this to return something like a list with your counts etc but the correct way to go would be declaring your logic in your `Index` method where you would return your view, instead of calling your `IActionResult Count` method just to get the counts and associated data. – Rahul Sharma Dec 17 '19 at 11:21
  • In my **IActionResult Index** am already have one logic to retrieve the all data in database ,So only am getting Confused and here is my Index `public IActionResult Index() { List lstProject = new List(); lstProject = objproject.GetAllProject().ToList(); return View(lstProject); }` now tell me how can i use the count – Sumo15 Dec 17 '19 at 11:29
  • My problem is I don't know how to retrieve count ,and how to retrieve all data in same page .... – Sumo15 Dec 23 '19 at 11:49
-1

Instead of Using Foreach in your view you can Use LINQ query to get the count in a variable and Passing the variable to ViewBag and get the count in View Using that ViewBag

Here is one Example:

ProjectDataAcces entities = new ProjectDataAcces();
var safety = entities.GetAllProject().Where(x => x.ProjectFamily.Equals("800XA incl Safety")).Count();
ViewBag.countsafety = safety;
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Your question was very very unclear on what you wanted. If you only wanted the `Count` of one `ProjectFamily` then your answer would suffice but if you wanted the `Count` of all your model (which you mentioned in the question) then you would need a `ForEach` loop to show your items. This line here `imagesList.Add` is the confusing line – Rahul Sharma Dec 27 '19 at 13:38