【问题标题】:How to clear Numeric field by Jquery in Razor View如何在 Razor 视图中通过 Jquery 清除数字字段
【发布时间】:2017-09-23 15:50:20
【问题描述】:

我有带有double 类型字段和string 的模型。

    [Display(Name = "Amount", ResourceType = (typeof(Resources.Work)))]
    public double Amount { get; set; }
    [Display(Name = "AmountInWords", ResourceType = (typeof(Resources.Work)))]
    public string AmountInWords{ get; set; }

在 ajax 调用之后,我在提交之前清除字段,即使它不是强制性属性,但它始终显示 Amount Field is Required 事件没有 Required 属性。

剃刀视图:

@Html.EditorFor(m => m.Amount, new { htmlAttributes = new { @class = "form-control", type = "number"} })
@Html.EditorFor(m => m.AmountInWords, new { htmlAttributes = new { @class = "form-control" } })

脚本

$("#Amount").val('');
$("#AmountInWords").val('');

【问题讨论】:

  • 你需要让底层类型可以为空double?
  • 你能分享你的View代码和你提到的ajax调用代码吗?
  • 如果它不是必需的,让它可以为空。
  • @Jasen 所以没有nullable 就无法解决?
  • 您不能将空表单值绑定到不可为空的属性。如果属性不能为空,为什么要发布空白值?

标签: jquery asp.net-mvc razor


【解决方案1】:

在模型中使用可空变量:double?

  [Display(Name = "Amount", ResourceType = (typeof(Resources.Work)))]
    public double? Amount { get; set; }

或:

@{ Html.EnableClientValidation(false); } 
@Html.EditorFor(m => m.Amount, new { htmlAttributes = new { @class = "form-control", type = "number"} })

【讨论】:

  • 然后必须更改整个项目才能获得金额的价值。它打破了变化。您还有其他解决方案吗?
  • 您没有其他解决方案,如果您想在没有验证的情况下发送。
  • 我已经在未经验证的情况下发送。你有没有在现场检查过没有意志。
  • 试试这个:@{ Html.EnableClientValidation(false); } @Html.EditorFor(m => m.Amount, new { htmlAttributes = new { @class = "form-control", type = "number"} })
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多