1

when I use the Html.EditorFor method, like this in mvc5:

@Html.EditorFor(model => model.RateHigh, new { htmlAttributes = new { @class = "form-control" } })

I get nice HTML like this:

<input class="form-control text-box single-line" data-val="true" data-val-number="The field tarief verbruik must be a number." data-val-required="'tarief verbruik' is een verplicht veld" id="RateNormal" name="RateNormal" type="text" value="0,15">

Now I want to show the value with 4 decimals, and the only way I found to do that is to create an editor template, is that correct?

Now my question is: do I have to type all the validation attributes and the extra class names myself in an editor template (because I start from scratch)?

That looks a little unlogical because when the asp.net mvc team decides to generate different html, I must change all my editortemplates also.

So what I am looking for as a sort of 'hook' in the standard code, that I can say: I want all the html for @Html.EditorFor, and then format the value in 4 decimals.

Is there a way to do this?

(so what I want generated is this: <input class="form-control text-box single-line" data-val="true" data-val-number="The field tarief verbruik must be a number." data-val-required="'tarief verbruik' is een verplicht veld" id="RateNormal" name="RateNormal" type="text" value="0,1500">

Michel
  • 23,085
  • 46
  • 152
  • 242

1 Answers1

2

You can define it in the model with data annotations.

Check this link out What is the proper data annotation to format my decimal property?

[DisplayFormat(DataFormatString="{0:#.####}")]

See Custom Format Strings for formats and DisplayFormatAttribute for examples

Community
  • 1
  • 1
user1666620
  • 4,800
  • 18
  • 27
  • That rocked! Is there also a possibillity to read the format string from a resource or constant (to make sure all `datformatstrings` in my viemodels are the same)? – Michel Jul 02 '14 at 07:10
  • Oh, that question was already answered here http://stackoverflow.com/questions/13576571/how-to-make-configurable-displayformat-attribute – Michel Jul 02 '14 at 07:13