5

小数点以下2桁のみが必要です。

@Html.TextBoxFor(m => m.Viewers, new { @tabindex = 7 })

次のような出力が必要です。

56.23

456.20

1.21

そのように..

4

5 に答える 5

3

ビューでエディター テンプレートを使用します。特定のビューの要件に合わせて特別に調整されたビュー モデルを定義します (この場合、小数点以下 2 桁に制限します)。

[DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]
public decimal Viewers{ get; set; }

または、次のようなモデルで正規表現を単純に使用できます

[RegularExpression(@"^\d+.\d{0,2}$")]
public decimal Viewers{ get; set; }

そしてhtmlで:

@Html.EditorFor(m => m.Viewers) 

またはTextBoxFor()正規表現でも動作します

@Html.TextBoxFor(m => m.Viewers, new { @tabindex = 7 })
于 2016-11-18T11:46:42.277 に答える
2

ビューで

@Html.TextBoxFor(m => m.Viewers, new { @tabindex = 7 }, new { Value=String.Format("{0:0.##}",Model.Viewers) })

コントローラーでも String.Format("{0:0.##}",Object.viewers) をフォーマットできます

オブジェクト - ビューに渡されるモデル (フィールド ビューアーを含む) を意味します。

これが役立つことを願っています

于 2016-11-18T11:45:44.960 に答える
1

クライアント側で小数を次のようにフォーマットすることをお勧めします。

あなたのViewModel

[DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]
public decimal Viewers { get; set; }

そしてあなたのビューで次を使用しますEditorFor

 @Html.EditorFor(m => m.Viewers, new { @tabindex = 7 })

この値が投稿されたらController、2 nums にトリムします。

検証が必要な場合は、正規表現を使用します。

[RegularExpression(@"^\d+\.\d{0,2}$")] //this line
[DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]
public decimal Viewers { get; set; }
于 2016-11-18T11:45:30.927 に答える
1

私が使用する場合。

String.Format("{0:0.00}", 123.4567);     

結果:

 // "123.46"

だからあなたはこれを試すことができます

@Html.TextBoxFor(m => String.Format("{0:0.00}", m.Viewers) , new { @tabindex = 7 })
于 2016-11-18T11:51:29.913 に答える
0

  $("#Viewers").change(function (e) {

            var num = parseFloat($(this).val());
            $(this).val(num.toFixed(2));
});
   @Html.TextBoxFor(m => m.Viewers, new { @tabindex = 7 })

返信ありがとうございます!!! :)

于 2016-11-18T13:07:12.717 に答える