2014-07-01 96 views
1

,當我在mvc5使用Html.EditorFor方法,像這樣:有沒有一種方法來定製Html.EditorFor生成的HTML?

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

現在我想展示與4位小數的值,只有這樣,我發現要做的是創建一個editor template,這是否正確?

現在我的問題是:是否必須自己在編輯器模板中鍵入所有驗證屬性和額外的類名(因爲我從頭開始)?

這看起來有些不合邏輯,因爲當asp.net mvc團隊決定生成不同的html時,我也必須更改所有的editortemplates。

所以我在尋找作爲標準代碼中的一種'鉤子',我可以說:我想要所有的html爲@Html.EditorFor,然後格式化值爲4位小數。

有沒有辦法做到這一點?

(所以我想產生是這樣的: <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">

回答

2

您可以用數據說明模型定義它

檢查此鏈接了What is the proper data annotation to format my decimal property?

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

請參閱自定義格式字符串格式和DisplayFormatAttribute示例

+0

那搖擺!是否還有可能從資源或常量中讀取格式字符串(以確保我的viemodels中的所有'datformatstrings'都是相同的)? – Michel

+0

哦,那個問題已經在這裏解答了http://stackoverflow.com/questions/13576571/how-to-make-configurable-displayformat-attribute – Michel

相關問題