【问题标题】:Get the count from database and show in html page从数据库中获取计数并显示在 html 页面中
【发布时间】:2019-12-17 05:47:50
【问题描述】:

ASP.NET MVC 应用程序,因此我启动了一个新程序,该程序包含基于组的数据库中的项目数,并且我使用了 ASP.NET MVC 来完成它。

我的模型类有以下详细信息

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; }
}

而Controller有如下代码

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);

    }

通过使用它,我必须在我的视图页面中获得计数。我该怎么做?

我的浏览页面是

                                <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>

在此我必须在 id="count1" id="count2" 地方显示计数....如何按组查看计数?

【问题讨论】:

标签: c# sql-server asp.net-mvc


【解决方案1】:

您可以使用foreach 循环来实现您的功能。 foreach 循环将遍历您的 List 并显示列表中的每个元素。具体到您的情况,您可以这样做:

  @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>
  }

【讨论】:

  • 这个很好,但是如果我使用控制器部分代码(即) IActionResult Count 我无法获得输出而不是 IActionResult Count 我使用 IActionResult Index 然后输出将是好的
  • 好吧,基本上你需要将Count分配给你正在做的相应组,然后你需要将Model发送给你相应的View。在您的情况下,您的 Count 方法将查找名为 Count 的视图,因此您可以这样做:return View("Index",imagesList);
  • @PakkiyaNathan 你需要检查你在你的imagesList 中得到了什么值,特别是Count = images[i].Count
  • 我正在使用一个名为 Project 的模型,因为我已经声明了计数并且在我的控制器文件中正在使用 IActionResult Count ..... ..而不是使用索引计数然后它显示正确的输出为什么
  • @PakkiyaNathan 您似乎感到困惑,您的IActionResult Count 不像普通方法。您可能希望这会返回类似于您的计数等的列表,但正确的方法是在您将返回视图的 Index 方法中声明您的逻辑,而不是调用您的 IActionResult Count 方法只是为了获得计数和相关数据。
【解决方案2】:

您可以使用LINQ 查询来获取变量中的计数并将变量传递给ViewBag 并在视图中使用ViewBag 获取计数,而不是使用Foreach

这是一个例子:

ProjectDataAcces entities = new ProjectDataAcces();
var safety = entities.GetAllProject().Where(x => x.ProjectFamily.Equals("800XA incl Safety")).Count();
ViewBag.countsafety = safety;

【讨论】:

  • 您的问题非常不清楚您想要什么。如果您只想要一个ProjectFamily 的Count,那么您的答案就足够了,但是如果您想要所有模型的Count(您在问题中提到),那么您需要一个ForEach 循环来显示您的项目。 imagesList.Add 这里的这一行是令人困惑的行
猜你喜欢
  • 2013-08-05
  • 1970-01-01
  • 2021-05-07
  • 1970-01-01
  • 2021-05-03
  • 2012-11-08
  • 2012-10-26
  • 2017-07-29
  • 1970-01-01
相关资源
最近更新 更多