locked
Razor Syntax for Division of 2 values RRS feed

  • Question

  • User2054207217 posted

    Hello:

    Having trouble performing simple division on 2 values in Razor on a view:

    Trying to divide a @Model.Sum with @Model.Count:

     @(@Model.Sum(i => i.MTDTotalSalesRollup)/@Model.Count())
    

    I am getting a Attempted to divide by zero. I am able to get valid values for @Model.Sum(i => i.MTDTotalSalesRollup) and @Model.Count()

    Any ideas? Thanks.

    Sunday, June 21, 2020 5:36 PM

Answers

  • User475983607 posted

    It's difficult to provide a solution without sample code that reproduces the issue.

    Working example

    @model List<MvcBasic.Models.CustomVm>
    @{
        ViewData["Title"] = "Index";
    }
    
        <div>
            @((decimal)Model.Sum(i => i.MTDTotalSalesRollup) / (decimal)Model.Count())
        </div>
    [HttpGet]
    public ActionResult Index()
    {
        List<CustomVm> vm = new List<CustomVm>()
        {
            new CustomVm()
            {
                    MTDTotalSalesRollup = 1
            },
            new CustomVm()
            {
                    MTDTotalSalesRollup = 3
            },
            new CustomVm()
            {
                    MTDTotalSalesRollup = 7
            },
            new CustomVm()
            {
                    MTDTotalSalesRollup = 9
            },
            new CustomVm()
            {
                    MTDTotalSalesRollup = 11
            }
        };
        return View(vm);
    }
        public class CustomVm
        {
            public int MTDTotalSalesRollup { get; set; }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 21, 2020 6:31 PM
  • User2054207217 posted

    This worked:

    @((decimal)Model.Count()==0 ? 0m : Model.Sum(i => i.MTDTotalSalesRollup) / (decimal)Model.Count())

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 22, 2020 1:27 AM

All replies

  • User475983607 posted

    It's difficult to provide a solution without sample code that reproduces the issue.

    Working example

    @model List<MvcBasic.Models.CustomVm>
    @{
        ViewData["Title"] = "Index";
    }
    
        <div>
            @((decimal)Model.Sum(i => i.MTDTotalSalesRollup) / (decimal)Model.Count())
        </div>
    [HttpGet]
    public ActionResult Index()
    {
        List<CustomVm> vm = new List<CustomVm>()
        {
            new CustomVm()
            {
                    MTDTotalSalesRollup = 1
            },
            new CustomVm()
            {
                    MTDTotalSalesRollup = 3
            },
            new CustomVm()
            {
                    MTDTotalSalesRollup = 7
            },
            new CustomVm()
            {
                    MTDTotalSalesRollup = 9
            },
            new CustomVm()
            {
                    MTDTotalSalesRollup = 11
            }
        };
        return View(vm);
    }
        public class CustomVm
        {
            public int MTDTotalSalesRollup { get; set; }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 21, 2020 6:31 PM
  • User2054207217 posted

    Thanks for your help. I still get the Attempt to divide by zero error.

    Also, I tried a new model/view based on your sample, but I get: ArgumentNullException: Value cannot be null. (Parameter 'source')

    @((decimal)Model.Sum(i => i.MTDTotalSalesRollup) / (decimal)Model.Count())

    Sunday, June 21, 2020 7:30 PM
  • User2054207217 posted

    My mistake, your sample code works fine for me. It's just when I plug in the division code that you have given, I still get the error in my original code.

    DivideByZeroException: Attempted to divide by zero.

     @((decimal)Model.Sum(i => i.MTDTotalSalesRollup) / (decimal)Model.Count())

    Any ideas to point me in the right direction?

    Sunday, June 21, 2020 7:49 PM
  • User-474980206 posted

    Model.Count can be zero, and divide by zero is an error. You need to handle this case. Probable something like

     @((decimal)Model.Sum(i => Model.Count() == 0 ? 0m : i.MTDTotalSalesRollup) / (decimal) Model.Count())

    Sunday, June 21, 2020 7:50 PM
  • User2054207217 posted

    Just figure it out, the Count was zero for some a certain selection, thus causing the error. I also just happened to see your reply Bruce, thanks! I will have to use mgebhard for marking as an answer.

    I do have a related question, is it possible to get the Model's property using @Model.PropertyName? I currently loop thorugh the individual items in the model to display in a table. But I also have one property value that does not change for the model, and would like to display it in the footer? Thanks.

    Sunday, June 21, 2020 8:07 PM
  • User2054207217 posted

    This worked:

    @((decimal)Model.Count()==0 ? 0m : Model.Sum(i => i.MTDTotalSalesRollup) / (decimal)Model.Count())

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 22, 2020 1:27 AM