【发布时间】: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