3

I am having a property in my Model. When I submit my form I received the below error.

Unable to cast object of type 'System.Decimal' to type 'System.Array'.

I am using MVC 5

public class PaymentInformationModel
{
    [Display(Name = "Payment Amount")]
    [Required(ErrorMessage = "Please enter the {0}")]
    [MaxLength(9)]
    [RegularExpression(@"^\d+.\d{0,2}$")]
    [Range(0, 9999999999999999.99)]
    public decimal PaymentAmount { get; set; }
}

What is wrong. I am entering normal number like 123.34.

Controller

[HttpPost]
public ActionResult Index(PaymentInformationModel model)
{
    if (ModelState.IsValid)
    {
        return View();
    }
    return View();
}

View

@model PaymentInformationModel

@using (Html.BeginForm("", "Payment", FormMethod.Post, new { Id = "Form1", @class = "form-horizontal" }))
{
    <div class="container">
        <div class="panel panel-default">
            <div class="panel-heading">Payment Information</div>
            <div class="panel-body">
                <div class="form-group">
                    @Html.LabelFor(x => x.PaymentAmount, new { @class = "control-label col-sm-2" })
                    <div class="input-group col-sm-3">
                        <span class="input-group-addon">$</span>
                        @Html.TextBoxFor(m => m.PaymentAmount, new { @class = "form-control col-sm-10" })
                    </div>
                    @Html.ValidationMessageFor(m => m.PaymentAmount, "", new { @class = "help-block" })
                </div>


            </div>
        </div>


    </div>
    <button type="submit" name="btnSubmit" id="btnSubmit" class="btn btn-success">PAY</button>
}
3
  • Can you post relevant parts of view and controller? Commented Jul 8, 2016 at 4:46
  • Just a guess, but does it have to do with your MaxLengthAttribute? Commented Jul 8, 2016 at 4:56
  • 1
    Apart from all this, you are using this overload of RangeAttribute class. I think you need to use this overload. Commented Jul 8, 2016 at 5:39

2 Answers 2

4

You are using MaxLength attribute, which does not work with decimal data types. I am not sure if RegularExpression attribute works either, but I did not verify that.

Try removing these two attributes and see if your code is working now. If it does - you might need to think of a way to use other attributes, that work correctly with decimal types (and Range validator seems a good candidate for that).

Just to see if MaxLength can be the issue, I looked at .NET source code. Here is a relevant part of code from IsValid method from MaxLengthAttribute with my comments:

var str = value as string;   // Your type is decimal so str is null after this line
if (str != null) {
    length = str.Length;  // <- This statement is not executed
}
else {
    // Next line is where you must be receiving an exception:
    length = ((Array)value).Length;
}
Sign up to request clarification or add additional context in comments.

3 Comments

@maxspan Then you will need to find a way how to implement your validation using other attributes. For Range try using other overload of attribute, like @Rohit416 mentioned in comment under your question
Yes I have used @Rohit416 trick. Dont know why he removed his comment
Oh, I should have kept it there, I thought it was not relevant could mislead because @dotnetom explained this well. Also i was not aware it was advised to be used. Besides I am posting that again for reference.
1

The culprit is Maxlength

public class PaymentInformationModel
    {
        [Display(Name = "Payment Amount")]
        [Required(ErrorMessage = "Please enter the {0}")]
        //[MaxLength(9)]
        [RegularExpression(@"^\d+.\d{0,2}$")]
        [Range(0, 9999999999999999.99)]
        public decimal PaymentAmount { get; set; }
    }

works fine for me.

Comments

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.