How to perform mathematical operation inside view in MVC

I am creating an MVC 5 web application for studies and I have a question: is it possible to perform a mathematical operation on View ?

Wanted to perform the multiplication of line 16. What would be the solution?

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.users.first_name_user) @Html.DisplayFor(modelItem => item.users.last_name_user)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.product.name_product)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.quantity)
    </td>
    <td>
        R$ @Html.DisplayFor(modelItem => item.product.price)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.quantity) * @Html.DisplayFor(modelItem => item.product.price) ******Preciso Realizar essa multiplicação****
    </td>
    <td>
        @Html.ActionLink("Editar", "Edit", new { id = item.id_order }) |
        @Html.ActionLink("Detalhes", "Details", new { id = item.id_order }) |
        @Html.ActionLink("Excluir", "Delete", new { id = item.id_order })
    </td>
</tr>
}

Table with results

I tried to use the code like this:

R$ @Html.DisplayFor(modelItem => item.quantity * item.product.price) 

Presented this error:

Error picture

Author: Maniero, 2020-03-21

1 answers

The ideal is not to do processing in view , so the correct solution is to create the model that already includes the total calculated value as a field of Model (it can be a viewmodel if you do not want to do in the normal model), and there you can use easily.

If you want the shape incorrect, but it works, you can do the count before and then use the result, something like this:

@{
foreach (var item in Model) {
    var total = item.quantity * item.product.price;
    ...
    @Html.DisplayFor(modelItem => total)
}

I put on GitHub for future reference.

 3
Author: Maniero, 2020-03-23 12:36:49