[英]Is there a way to customize the HTML generated by the Html.EditorFor?
當我使用Html.EditorFor
方法時,例如在mvc5中:
@Html.EditorFor(model => model.RateHigh, new { htmlAttributes = new { @class = "form-control" } })
我得到這樣的漂亮HTML:
<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">
現在,我想用四位小數顯示該值,而我發現做到這一點的唯一方法是創建一個editor template
,對嗎?
現在我的問題是:我是否必須在編輯器模板中鍵入所有驗證屬性和我自己的額外類名(因為我從頭開始)?
這看起來有些不合邏輯,因為當asp.net mvc團隊決定生成不同的html時,我還必須更改所有的editortemplates。
因此,我想要說的是標准代碼中的一種“鈎子”,我可以說:我想要@Html.EditorFor
所有html,然后將值格式化為4 @Html.EditorFor
。
有沒有辦法做到這一點?
(因此,我想生成的是: <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">
您可以在模型中使用數據注釋對其進行定義。
檢查此鏈接, 什么是正確的數據注釋以格式化我的十進制屬性?
[DisplayFormat(DataFormatString="{0:#.####}")]
有關格式,請參見“自定義格式字符串”,有關示例,請參見DisplayFormatAttribute。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.