I’m working on a ASP.NET MVC 5 Application. I’m using Attributes to validate my models (back- and frontend).
One of my models has a property which looks like this:
[Required( ... )]
[Range( 0d, Double.MaxValue, ...)]
public Decimal Amount { get; set; }
All values from 0 to Double.MaxValue are allowed as amount. (This works perfectly in the backend)
In my UI I have a form for this model:
@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>
}
This code creates HTML which looks like this:
<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>
Now the problem:
Based on the validation attributes the HtmlHelper creates a jQuery validation rule for max range: data-val-range-max="1.79769313486232E+308". This causes jQuery validate to allow only values from 0 to 1.79…. (values smaller than 2)
How can I fix this?