Share via


Need of addition calculation in cshtml file

Question

Wednesday, July 3, 2013 12:41 AM

viewclass:    sgb.cshtml

how to do the arithmetic calculation in cshtml file.

                @foreach (MSService.ViewModels.StudentGradeBook x in Model.Student)
                {                   
                    <td>@x.PointsAwarded /@x.PointsPossible </td>

here i need the sum caluculation for pointsawarded and pointspossible
                }

                  <td>totalpointswarded/totalpointspossible**(here i need** to dispaly the total values)</td>

**totalpointswarded=sum of @x.Pointsawrded
**

****totalpointspossible=sum of @.PointsPossible


All replies (3)

Wednesday, July 3, 2013 1:56 AM âś…Answered

Hi,

Instead of creating sum in the cbhtml file. Create sum of that value in the variable and assign that variable in viewbag like 

in Action Mehtod of controller create for loop and caculate the sum.

ViewBag.Total =sumvaribale

and display viewbag like following.

@ViewBag.Total

Wednesday, July 3, 2013 1:58 AM

Hi vidhya this might help you read this care fully!!

http://stackoverflow.com/questions/4386830/asp-net-mvc-razor-multiply-two-item-and-convert-to-string

and

http://stackoverflow.com/questions/6633377/mvc3-razor-html-textboxfor-calculated-fields


Wednesday, July 3, 2013 10:04 AM

Try using Linq to display the total sum of PointsAwarded and PointsPossible

<td>@Model.Student.Sum(e => e.PointsAwarded) / @Model.Student.Sum(e => e.PointsPossible)</td>