【问题标题】:Error on site with mathematical operation on ASP .NET core在 ASP .NET 核心上进行数学运算时出现现场错误
【发布时间】:2021-08-26 14:04:51
【问题描述】:

我从我的数据库中有一些东西的计数和百分比,并在现场显示它们。(使用数学运算) 但是如果数据库是空的,我会看到这个错误:

adminContentCount.UserPercentage = (currentMonthUser * 100) / userCount; 这是我的服务:

 CountViewModel adminContetnCount = new CountViewModel()
        {
            
           UserCount = await _userRepository.UserCount(),
       
            CurrentMonthUser = await _userRepository.CurrentMonthUser(),

      
        };

          

这是我的控制器:

public async Task<IActionResult> Index()
    {
        var adminContentCount = await _siteService.AdminContentCount();
    
        var userCount = adminContentCount.UserCount;
        var currentMonthUser = adminContentCount.CurrentMonthUser;

        return View(adminContentCount);
    }

我在服务和控制器上使用它,然后将在视图中显示

如何在我的服务中检查这一点。我从数据库中得到它们的数量和百分比。

【问题讨论】:

    标签: asp.net-core math percentage operation


    【解决方案1】:

    我没有看到您在此处发布的错误,但我假设它是除以 0。因此

    adminContentCount.UserPercentage = userCount == 0 ? 0 : (currentMonthUser * 100) / userCount;
    

    【讨论】:

    • 非常感谢。是的,这是除以 0 的平均值,所以我看到了错误:adminContentCount.UserPercentage = (currentMonthUser * 100) / userCount;这是我的服务:所以如果只检查用户帐户?而且我也没有检查 currentMonthUser?
    • 你不需要检查另一个。数学不允许你除以 0,但你可以乘以它,它仍然是 0。 - 如果你在任何地方除以它,请在那里检查。 =)
    猜你喜欢
    • 1970-01-01
    • 2014-09-30
    • 2022-12-09
    • 2013-05-04
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 2010-12-17
    • 2015-12-27
    相关资源
    最近更新 更多