Click here to Skip to main content
15,064,665 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried to calculate the percentage of total count in viewbag after i click on the submit button. but the program is displaying the value before the submit button is clicked.

i understand the error as it says that error is because the system tried to divide the number by zero. but i dont have any idea on how to make it correctly. hope you can help as i'm still new in this language

What I have tried:

in view:

<tr style="text-align:center">
                            <td>@ViewBag.datepick.Count</td>
                            <td>@ViewBag.pass.Count</td>
                            <td></td>
                        </tr>


in controller:

public ActionResult TesterPerformance(string name, DateTime? start, DateTime? end)
       {


           var selectdate = (from c in db.Information
                             join t in db.Testers
                             on c.TesterID equals t.TesterID
                             join i in db.Parameters
                             on c.Stag_status equals i.ParameterID
                             where  c.Time_start >= start && c.Time_start <= end
                             select new DetailBundle
                             {
                                 Time_start = c.Time_start,
                                 Time_fail = c.Time_fail,
                                 Time_pass = c.Time_pass,
                                 Param_desc = i.Param_desc,
                                 TesterName = t.TesterName,
                                 Service_tag = c.Service_tag

                             }).ToList();
           ViewBag.datepick = selectdate;
           var selectpass = (from c in db.Information
                             join t in db.Testers
                             on c.TesterID equals t.TesterID
                             join i in db.Parameters
                             on c.Stag_status equals i.ParameterID
                             where c.Time_start >= start && c.Time_start <= end &&  i.Param_desc=="pass"
                             select new DetailBundle
                             {
                                 Time_start = c.Time_start,
                                 Time_fail = c.Time_fail,
                                 Time_pass = c.Time_pass,
                                 Param_desc = i.Param_desc,
                                 TesterName = t.TesterName,
                                 Service_tag = c.Service_tag

                             }).ToList();
           ViewBag.pass = selectpass;

           ViewBag.Percentage = (ViewBag.pass.Count / ViewBag.datepick.Count) * 100;
           return View();
       }


the submit button is in the same view
Posted
Updated 16-Jul-20 0:23am
Comments
F-ES Sitecore 16-Jul-20 6:51am
   
Is the problem that it shows the "old" value or that you're getting a divide by zero? Show the mark-up where the value\inputs are shown and also indicate exactly what line throws the error.
Laxmidhar tatwa technologies 16-Jul-20 11:20am
   
ViewBag.Percentage = ViewBag.datepick.Count <= 0 ?ViewBag.pass.Count : (ViewBag.pass.Count / ViewBag.datepick.Count) * 100;

1 solution

You'll need to update your code to account for the possibility that the denominator could be zero even after the form is submitted.

You'll also need to adjust the calculation - currently you're performing integer division, so your result will either be 0% or 100%.

Try:
C#
ViewBag.Percentage = selectdate.Count == 0 ? 0D : 100D * (double)selectpass.Count / selectdate.Count;
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900