如何在ASP.NET MVC中减去两个值?

问题描述 投票:-2回答:3

我有3个值,即AmountOfRent,AmountPaid和RemainingAmount。

我如何减去租金金额和支付金额以获得剩余金额的价值

AmountOfRent - AmountPaid = RemainingAmount

我不知道如何在mvc中编写代码。

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

<td>
    @Html.DisplayFor(modelItem => item.AmountPaid)
</td>

<td>
    @Html.DisplayFor(modelItem => item.RemainingAmount)
</td>
c# asp.net-mvc count subtraction
3个回答
0
投票

让我们假设这是您的控制器的操作方法,它将模型返回到您的视图:

public ActionResult Something()
{
    //. . .
    var model = manager.GetModel();
    model.RemainingAmount = model.AmountOfRent - model.AmountPaid;
    return View(model);
}

甚至你可以让你的财产返回你想要的结果:

private int remainingAmount;

public int RemainingAmount
{
    get { return AmountOfRent - AmountPaid; }
    set { remainingAmount = value; } //this may not be needed
}

0
投票

您可以在模型中声明以计算剩余金额

public decimal AmountOfRent { get; set; }

public decimal AmountPaid { get; set; }

public decimal RemainingAmount => AmountOfRent - AmountPaid;

0
投票

如果你想在cshtml文件中执行它,那么下面是解决方案:

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

<td>
    @Html.DisplayFor(modelItem => item.AmountPaid)
</td>

<td>
    @Html.DisplayFor(modelItem => item.RemainingAmount)
</td>
<td>
@{
    var result= item.contracts.AmountOfRent - item.AmountPaid;
    Html.Display(result)
}
</td>
© www.soinside.com 2019 - 2024. All rights reserved.