Software Developer
Software Developer

Reputation: 103

How Subtract 2 values in ASP.NET MVC

i want to Subtract 2 values and show the result in the index .

The AmountOfRent is AmountOfRent , and the Receipt the user will enter in the Crete , so when back to the index the Balance will show

this is my Controller:

    [HttpPost]

    public ActionResult Index(ContractMonth am)
    {

        am.Balance = am.contracts.AmountOfRent - am.Receipt;

        return View(am);
    }

This is the view :

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Monthe)
        </th>
     
        <th>
            @Html.DisplayNameFor(model => model.Receipt)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Balance)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ContractsId)
        </th>
        <th></th>
    </tr>


@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Monthe)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.contracts.AmountOfRent)
        </td>
   
        <td>
            @Html.DisplayFor(modelItem => item.Receipt)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Balance)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.contracts.Contract_Num)
        </td>
       
    </tr>
}

</table>

Upvotes: 1

Views: 1238

Answers (1)

habib
habib

Reputation: 2446

If you just want to subtract two numbers and show there result in view you can do this in razor view by doing this.

<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.Monthe)
    </th>

    <th>
        @Html.DisplayNameFor(model => model.Receipt)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Balance)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ContractsId)
    </th>
    <th></th>
</tr>


@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Monthe)
    </td>
    <td>
        @{ var reuslt=item.contracts.AmountOfRent-item.Receipt;}
        @result
    </td>

    <td>
        @Html.DisplayFor(modelItem => item.Receipt)
    </td>
    <td>
        @item.AmountOfRent - item.Receipt
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.contracts.Contract_Num)
    </td>

</tr>
}

</table>

Upvotes: 1

Related Questions