0

I have tried both this:

[Display(Name = "Contract Value")]
[DisplayFormat(DataFormatString = "{0.##}")]
public decimal ContractValue { get; set; }

And this:

[Display(Name = "Contract Value")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0.##}")]
public decimal ContractValue { get; set; }

View:

@Html.LabelFor(x => contractInfoModel.ContractValue)
@Html.TextBoxFor(x => contractInfoModel.ContractValue, new { maxlength = "100" })

If I do this, it works. But I don't want to do this:

<input type="text" name="@Html.NameFor(x=>contractInfoModel.ContractValue)" id="@Html.IdFor(x=>contractInfoModel.ContractValue)" value="@Model.ContractInfoM.ContractValue.ToString("0.##")" class="numeric" maxlength="10" />

The Display Name works great. But it still shows 4 decimal places. What am I doing wrong?

Thanks!

Casey Crookston
  • 13,016
  • 24
  • 107
  • 193

1 Answers1

0

Found it:

By design, the DisplayFormat is not honored in a TextBoxFor. So instead of this:

@Html.TextBoxFor(x => contractInfoModel.ContractValue, new { maxlength = "10" })

I did this:

@Html.EditorFor(x => contractInfoModel.ContractValue, new { maxlength = "10" })

And it works great.

Casey Crookston
  • 13,016
  • 24
  • 107
  • 193
  • 1
    If you want to use `TextBoxFor()` its `@Html.TextBoxFor(x => contractInfoModel.ContractValue, "{0.##}", new { maxlength = "10" })` –  Nov 04 '16 at 21:47