【问题标题】:How subtract two value in ASP.NET MVC?如何在 ASP.NET MVC 中减去两个值?
【发布时间】:2019-03-12 08:45:08
【问题描述】:

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

如何减去 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>

【问题讨论】:

  • 你的意思是(item.contracts.AmountOfRent - item.AmountPaid) 吗?
  • 在控制器中计算该值?
  • (item.contracts.AmountOfRent - item.AmountPaid) 是的,我的意思是
  • 我该怎么做?可以写代码
  • 首先学习如何编码 ;)

标签: c# asp.net-mvc count subtraction


【解决方案1】:

您可以在模型中声明计算剩余数量

public decimal AmountOfRent { get; set; }

public decimal AmountPaid { get; set; }

public decimal RemainingAmount => AmountOfRent - AmountPaid;

【讨论】:

  • 那我就不写别的了?
  • 如果用户可以输入 AmountOfRent 和 AmountPaid 2 个属性,则需要在客户端添加事件重新计算 RemainingAmount。
  • 我该怎么做?
【解决方案2】:

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

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
}

【讨论】:

  • 经理是什么意思?因为这个词下面有读行
  • 这只是一个示例,它意味着无论您在何处获取或创建对象。
  • 对不起,我不明白,能告诉我我必须写什么吗?
【解决方案3】:

如果你想在 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>

【讨论】:

  • 它不会来
  • 请附上即将发生的截图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-21
  • 1970-01-01
  • 2020-07-21
  • 1970-01-01
相关资源
最近更新 更多