2015年4月22日 星期三

DisplayTemplates & EditorTemplates

在mvc中,我們可以為特定型別定義模板

當在razor語法中,用某些語法呼叫該型別時,會自動對應到Shared資料夾下的模板來顯示


@Html.DisplayFor ->Shared/DisplayTemplates/型別名稱
@Html.EditorFor->Shared/EditorTemplates/型別名稱

例如,當我們在razor中使用@Html.DisplayFor(m=>m.myText),myText是string型別時,會使用Shared/DisplayTemplates/String.cshtml來取代該字串的顯示

以上的模板是針對網站上所有該型別的資料,如果只是想要讓特定幾筆資料使用模板,可以將Shared/DisplayTemplates/下的String.cshtml改名,然後再到model使用的型別上定義要使用的模板


如String.cshtml->MyString.cshtml

model class

[UIHint("MyString")]
public string myText{ get; set;}



沒有留言:

張貼留言