简体   繁体   中英

Is there a way to customize the HTML generated by the Html.EditorFor?

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">

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM