【问题标题】:decimal format string is null in controller mvc razor c#十进制格式字符串在控制器mvc razor c#中为空
【发布时间】:2018-02-09 08:17:35
【问题描述】:

我在部分视图中有 TextboxFor,我想显示十进制格式示例(3,108,000.00)。用于显示但希望我通过 Ajax 调用控制器的值为空。谢谢你的帮助。

@Html.TextBoxFor(x => x.List_TRNQuotationLeasing.CashBook, new { @class = "numeric form-control right", @Value = Model.List_TRNQuotationLeasing.CashBook != null ? Model.List_TRNQuotationLeasing.CashBook.Value.ToString("#,##0.00") : "" })

【问题讨论】:

  • 你能把你的 Ajax 代码放进去吗?
  • 您可以将值作为最小值提交,然后在服务器端进行处理吗?因此,例如,如果该值以 GBP 为单位并且为 3,108,000.00,您将提交 310800000 - 即3,108,000.00 * 100 以便士表示。然后,您将服务器上的该值除以 100 以获得原始值
  • 否则你可以走自定义模型绑定器路线 - 这可能有点矫枉过正 - stackoverflow.com/questions/37389084/…

标签: c# asp.net-mvc razorengine html.textboxfor


【解决方案1】:

如果您尝试这样做,您不能将900,000.00 之类的值直接发布到decimal。您需要在发送前重新格式化号码并去掉 ,,在 ViewModel 中使用 string 并在发布后应用转换,或者在请求期间使用特殊的 ASP.NET MVC Model Binder 进行转换。

例如,如果您想使用选项 1,您可以在发送之前在 JavaScript 中执行此操作 (jQuery):

 // ...before send...
 // e.g. apply to all fields marked with "numeric"
 $("input.numeric").each(function() {
    numericWithOutComma = $(this).val().replace(",", "");

    // override text... yes it will lose the format. You could enhance that later
    $(this).val(numericWithOutComma);
 });

 // Execute Ajax send...

此外,如果您想使用选项 3,还有这个 interesting discussion

【讨论】:

    猜你喜欢
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 2022-07-22
    • 1970-01-01
    • 1970-01-01
    • 2014-11-13
    • 2016-03-06
    • 2022-07-08
    相关资源
    最近更新 更多