私のモデルにプロパティがあります。私のフォームを提出すると、私は以下のエラーを受け取りました。MVCで表示するDecimalプロパティをバインドできません
タイプ '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)を使用しています。私はあなたが[このオーバーロード](https://msdn.microsoft.com/en-us/library/cc679255(v = vs.110).aspx)を使う必要があると思います。 – Rohit416