maxspan
maxspan

Reputation: 14147

Cannot bind Decimal property to view in MVC

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>
}

Upvotes: 3

Views: 1000

Answers (2)

Sree Harsha Nellore
Sree Harsha Nellore

Reputation: 438

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.

Upvotes: 1

dotnetom
dotnetom

Reputation: 24901

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;
}

Upvotes: 4

Related Questions