C# 在MVC核心视图中显示计算

C# 在MVC核心视图中显示计算,c#,entity-framework-core,asp.net-core-mvc,percentage,C#,Entity Framework Core,Asp.net Core Mvc,Percentage,我试图在视图中显示百分比计算的结果。 Round和Score来自本地数据库,我想计算记录的轮数,将Score中存储的值相加,然后以百分比形式输出结果。我希望这是有道理的 目前,我在数据库中存储了一些值,但我一直得到的结果是零。数据库中的值似乎没有进入计算方法 非常感谢 控制器: public ActionResult CalcPercent() { PracticeViewModel pvm = new PracticeViewModel();

我试图在视图中显示百分比计算的结果。 Round和Score来自本地数据库,我想计算记录的轮数,将Score中存储的值相加,然后以百分比形式输出结果。我希望这是有道理的

目前,我在数据库中存储了一些值,但我一直得到的结果是零。数据库中的值似乎没有进入计算方法

非常感谢

控制器:

    public ActionResult CalcPercent()
    {
        PracticeViewModel pvm = new PracticeViewModel();
        
        var rounds = _context.Practices3.Select(r => r.Round).Count();
        var scores = _context.Practices3.Sum(s => s.Score);
        pvm.Percentage = scores / (rounds * 10) * 100;

        return View(pvm);
    }
型号:

public class Practice
{
    [Key]
    public int Id { get; set; }
    public DateTime Date { get; set; }
    public int Score { get; set; }
    public double Round { get; set; }

}
视图模型:

public class PracticeViewModel
{
    public int Percentage { get; set; }
}
视图:

@model EmailAndPDF\u Test.Models.PracticeViewModel
@Model.Percentage


错误公共整数百分比{get;set;};您必须将其更改为公共双百分比{get;set;}

为什么取而代之的是var rounds=_context.Practices3.Select(r=>r.Round.Count(); 您没有使用上下文。Practices3.Count();也许你的意思是_context.Practices3.Where(r=>r.Round>0.Count()

尝试代替pvm。百分比=分数/(轮数*10)*100;使用 pvm.百分比=数学轮((分数/(轮数*10))*100,3)

@model EmailAndPDF_Test.Models.PracticeViewModel

<p> @Model.Percentage</p>