2016-07-08 5 views
3

私のモデルにプロパティがあります。私のフォームを提出すると、私は以下のエラーを受け取りました。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> 
} 
+0

ビューとコントローラの関連部分を投稿できますか? –

+0

ちょうど推測ですが、あなたの 'MaxLengthAttribute'と関係がありますか? –

+1

これ以外にも、「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

答えて

4

あなたは、小数点データ型では動作しませんMaxLength属性を使用しています。 RegularExpression属性が動作するかどうかはわかりませんが、確認しませんでした。

これら2つの属性を削除し、コードが正常に機能しているかどうかを確認してください。そうであれば、他の属性を使う方法を考えなければならないかもしれませんが、十進数型で正しく動作します(そしてRangeバリデーターがそれに適しているようです)。

MaxLengthが問題になるかどうかを確認するには、.NET source codeを見てください。ここに私のコメントをMaxLengthAttributeからIsValid方法からコードの関連する部分である:

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

MaxLengthとRangeを削除すると動作します。 – maxspan

+0

@maxspan次に、他の属性を使用して検証を実装する方法を見つける必要があります。 'Range'については、あなたの質問の下でコメントに記載されている@ Rohit416のような属性の他のオーバーロードを試してみてください。 – dotnetom

+0

はい、私は@ Rohit416トリックを使用しました。なぜ彼が彼のコメントを削除したのか分からない – maxspan

1

犯人は私のために正常に動作します

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; } 
    } 
です。

関連する問題