我在我的模型中有一个属性。当我提交表单时,我收到了以下错误。无法绑定Decimal属性在MVC中查看
无法投射“System.Decimal”类型的对象以键入“System.Array”。
我使用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; }
}
什么是错的。我输入正常数字123.34。
控制器
[HttpPost]
public ActionResult Index(PaymentInformationModel model)
{
if (ModelState.IsValid)
{
return View();
}
return 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>
}
你可以发布视图和控制器的相关部分吗? –
只是猜测,但它与你的'MaxLengthAttribute'有关吗? –
除此之外,您正在使用RangeAttribute类的[this overload](https://msdn.microsoft.com/en-us/library/cc679307(v = vs.110).aspx)。我认为你需要使用[this overload](https://msdn.microsoft.com/en-us/library/cc679255(v = vs.110).aspx)。 – Rohit416