我在我的模型中有一個屬性。當我提交表單時,我收到了以下錯誤。無法綁定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