4

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?

1 Answer 1

2

One way that i know is this:

      [Range(typeof(decimal), "0", "79228162514264337593543950335")]
      public decimal Amount { get; set; }

Meh-ish, but this is a real deal.

And decimal MaxValue is 79228162514264337593543950335. https://msdn.microsoft.com/en-us/library/system.decimal.maxvalue(v=vs.110).aspx

Sign up to request clarification or add additional context in comments.

4 Comments

Bro' this will work. decimal is between -79,228,162,514,264,337,593,543,950,335 and 79,228,162,514,264,337,593,543,950,335. You want > 0 values. This will work. 1000%
That's why i commented there about decimal MaxValue. What Double.MaxValue has to do with what you're trying to achieve?
Sorry you're right. I tested it again and it worked… (I made a stupid mistake in my previous test i used d(Double) instead of M(Decimal) … which resulted in an overflow exception)
Great. If you have any more questions, please update your requirements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.