我正在研究ASP.NET MVC 5应用程序.我正在使用Attributes来验证我的模型(back-and frontend).
我的一个模型有一个如下所示的属性:
[Required( ... )]
[Range( 0d, Double.MaxValue, ...)]
public Decimal Amount { get; set; }
允许从0到Double.MaxValue的所有值作为金额. (这在后端非常有效)
在我的UI中,我有一个此模型的表单:
@using (Html.BeginForm(null, null, FormMethod.Post))
{
@* Anti Forgery Token *@
@Html.AntiForgeryToken()
<fieldset>
...
<section>
@Html.LabelFor(model => model.Amount, new { @class = "label" })
<label class="input">
@Html.EditorFor(model => model.Amount, new { htmlAttributes = new { placeholder = Resources.Amount } })
@Html.RenderCombinedValidationMessages(x => x.Amount, false)
</label>
</section>
</fieldset>
@Html.RenderGlobalValidationMessages()
<footer>
<div class="btn-group">
<a href="@Html.GetPreviousPage( Url.Action( "Index", "Controller" ) )" class="btn btn-default">@Resources.Back</a>
<button type="submit" class="btn btn-primary">@Resources.Create</button>
</div>
</footer>
}
此代码创建HTML,如下所示:
<form action="/Controller/Create" class="smart-form" method="post" novalidate="novalidate">
<fieldset>
<section>
<label class="label" for="Amount">Amount</label>
<label class="input">
<input
data-val="true" data-val-number="The field Amount must be a number."
data-val-range="Amount must be between 0 and 1.79769313486232E+308."
data-val-range-max="1.79769313486232E+308"
data-val-range-min="0"
data-val-required="Amount is required."
id="Amount" name="Amount" placeholder="Amount" type="text" value="">
<ul class="validationMessageList" data-property-name="Amount"></ul><span class="field-validation-valid" data-valmsg-for="Amount" data-valmsg-replace="true"></span>
</label>
</section>
</fieldset>
<footer>
<div class="btn-group">
<a href="http://localhost:26165/Controller/Create" class="btn btn-default">Back</a>
<button type="submit" class="btn btn-primary">Create</button>
</div>
</footer>
</form>
现在的问题是:
基于验证属性,HtmlHelper为最大范围创建jQuery验证规则:data-val-range-max =“1.79769313486232E 308”.这导致jQuery验证仅允许从0到1.79的值…. (小于2的值)
我怎样才能解决这个问题?
最佳答案 我知道的一种方式是:
[Range(typeof(decimal), "0", "79228162514264337593543950335")]
public decimal Amount { get; set; }
嗯,但这是一个真正的交易.
十进制MaxValue是79228162514264337593543950335.https://msdn.microsoft.com/en-us/library/system.decimal.maxvalue(v=vs.110).aspx