【发布时间】:2021-02-17 10:54:09
【问题描述】:
我正在尝试在视图中显示百分比计算的结果。 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_Test.Models.PracticeViewModel
<p> @Model.Percentage</p>
【问题讨论】:
标签: c# entity-framework-core asp.net-core-mvc percentage