You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于Debtors输入值计算并绑定CashDebtorsPercent字段值?

问题分析与修复方案

现有代码的问题

  • 第一个label的class属性语法错误:class="> 属于无效写法,会导致HTML解析异常。
  • 仅为30-60、60-90天的输入框绑定了变更事件,但缺少120天、120天以上的输入控件,对应变量DebtorsPercent120、DebtorsPercent130没有输入入口,计算时会默认取0,结果不准确。
  • @bind-Value与@oninput同时使用存在冲突:@bind-Value内部已经包含了值变更的绑定逻辑,额外绑定@oninput会导致触发时机异常。
  • GetPercentage方法声明为async Task但内部无异步操作,属于冗余写法。
  • 现金债务人占比的输入框未设置禁用,用户可手动修改,违背“自动计算”的需求。

修复后的完整代码

<div>
    <label for="DebtorsP30">Debtors(%) 0-30</label>
    <div>
        <InputNumber class="form-control" @bind-Value=CashDebtorsPercent Disabled=true />
    </div>
</div>
<div>
    <label for="DebtorsP60">Debtors(%) 30-60</label>
    <div>
        <InputNumber class="form-control" @bind-Value=DebtorsPercent60 @bind-Value:after="CalculateCashDebtors" />
    </div>
</div>                            
<div>
    <label for="DebtorsP90">Debtors(%) 60-90</label>
    <div>
        <InputNumber class="form-control" @bind-Value=DebtorsPercent90 @bind-Value:after="CalculateCashDebtors" />
    </div>
</div>
<div>
    <label for="DebtorsP120">Debtors(%) 90-120</label>
    <div>
        <InputNumber class="form-control" @bind-Value=DebtorsPercent120 @bind-Value:after="CalculateCashDebtors" />
    </div>
</div>
<div>
    <label for="DebtorsP130">Debtors(%) 120以上</label>
    <div>
        <InputNumber class="form-control" @bind-Value=DebtorsPercent130 @bind-Value:after="CalculateCashDebtors" />
    </div>
</div>

@code {
    private decimal CashDebtorsPercent, DebtorsPercent60, DebtorsPercent90, DebtorsPercent120, DebtorsPercent130;

    private void CalculateCashDebtors()
    {
        CashDebtorsPercent = 100 - (DebtorsPercent60 + DebtorsPercent90 + DebtorsPercent120 + DebtorsPercent130);
        // 确保数值在合理范围(0-100)内
        CashDebtorsPercent = Math.Clamp(CashDebtorsPercent, 0, 100);
    }
}

关键修复点说明

  • 修正HTML标签语法错误,给所有输入框统一添加form-control样式类。
  • 补充120天、120天以上的输入控件,并为所有可编辑输入框绑定@bind-Value:after事件,确保值变更后立即触发计算。
  • 将现金债务人占比的输入框设为Disabled=true,防止用户手动修改,保证计算结果的唯一性。
  • 移除冗余的async声明,将计算方法改为同步方法,添加Math.Clamp确保结果在0-100的合理区间。

内容的提问来源于stack exchange,提问作者Zabron Muyambo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 04:25:15