2011-02-11 130 views

回答

8

下面的博客文章介紹如何自定義編輯模板:http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html

基本上,你必須添加一個名爲Views\Shared\EditorTemplates\Object.cshtml文件,並把所有的邏輯用於顯示對象那裏。

+0

我在尋找原始模板,這樣我就可以改變什麼,我需要改變,而不是從開始寫吧,反正我想我就使用aspx,並將其轉換爲剃鬚刀。 – ryudice 2011-02-11 17:38:18

4

當@marcind說他們被編譯進來時,模板本身沒有嵌入,而是寫在代碼中。例如,EditorFor調用TemplateFor,這可能會調用TextAreaExtensions.TextArea或許多其他擴展中的一個生成最終輸出的代碼。這可能是因爲我們可以選擇刪除默認視圖引擎並使用類似nhaml的東西。

模板名稱和創建結果輸出的函數之間的映射可以在System.Web.Mvc.Html.TemplateHelpers中看到。另見System.Web.Mvc.Html.DefaultEditorTemplates

現在存在的最接近的東西是存在於Mvc3Futures中的Webforms模板,它可在aspnet.codeplex.com website上找到。它內部存在一個包含模板的DefaultTemplates \ EditorTemplates文件夾。

這裏的Object.ascx模板:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> 
<script runat="server"> 
    bool ShouldShow(ModelMetadata metadata) { 
     return metadata.ShowForEdit 
      && metadata.ModelType != typeof(System.Data.EntityState) 
      && !metadata.IsComplexType 
      && !ViewData.TemplateInfo.Visited(metadata); 
    } 
</script> 
<% if (ViewData.TemplateInfo.TemplateDepth > 1) { %> 
    <% if (Model == null) { %> 
     <%= ViewData.ModelMetadata.NullDisplayText %> 
    <% } else { %> 
     <%= ViewData.ModelMetadata.SimpleDisplayText %> 
    <% } %> 
<% } else { %>  
    <% foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => ShouldShow(pm))) { %> 
     <% if (prop.HideSurroundingHtml) { %> 
      <%= Html.Editor(prop.PropertyName) %> 
     <% } else { %> 
      <% if (!String.IsNullOrEmpty(Html.Label(prop.PropertyName).ToHtmlString())) { %> 
       <div class="editor-label"><%= Html.Label(prop.PropertyName) %></div> 
      <% } %> 
      <div class="editor-field"><%= Html.Editor(prop.PropertyName) %> <%= Html.ValidationMessage(prop.PropertyName, "*") %></div> 
     <% } %> 
    <% } %> 
<% } %> 
相關問題