如何在Razor View中通过Jquery清除数字字段

时间:2017-09-22 17:12:32

标签: jquery asp.net-mvc razor

我的模型包含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属性。

Razor查看:

@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('');

1 个答案:

答案 0 :(得分: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"} })