2014-02-27 60 views
2

突然我的MVC應用程序停止使用自定義EditorForDisplayFor模板我有。我不確定自從我更改用戶界面以來它究竟是什麼時候失敗了。我有DisplayTemplatesEditorTemplates下的Shared文件夾中的模板。我做覆蓋ViewEnginesCollectionGlobal.asax這一點:ASP.NET MVC 5.1 EditorFor和DisplayFor不使用自定義模板

ViewEngines.Engines.Clear(); 
ViewEngines.Engines.Add(new CSHtmlRazorViewEngine { 
    PartialViewLocationFormats = new string[] { 
     "~/Views/Shared/EditorTemplates/{0}.cshtml", 
     "~/Views/Shared/Partials/{0}.cshtml" 
    } 
}); 

其中CSHtmlRazorViewEngine是:

public sealed class CSHtmlRazorViewEngine : RazorViewEngine { 
    public CSHtmlRazorViewEngine() 
     : base() { 
     this.AreaViewLocationFormats = new string[2] { 
      "~/Areas/{2}/Views/{1}/{0}.cshtml", 
      "~/Areas/{2}/Views/Shared/{0}.cshtml" 
     }; 
     this.AreaMasterLocationFormats = new string[2] { 
      "~/Areas/{2}/Views/{1}/{0}.cshtml", 
      "~/Areas/{2}/Views/Shared/{0}.cshtml" 
     }; 
     this.AreaPartialViewLocationFormats = new string[2] { 
      "~/Areas/{2}/Views/{1}/{0}.cshtml", 
      "~/Areas/{2}/Views/Shared/{0}.cshtml" 
     }; 
     this.ViewLocationFormats = new string[3] { 
      "~/Views/{0}.cshtml", 
      "~/Views/{1}/{0}.cshtml", 
      "~/Views/Shared/{0}.cshtml" 
     }; 
     this.MasterLocationFormats = new string[2] { 
      "~/Views/{1}/{0}.cshtml", 
      "~/Views/Shared/{0}.cshtml" 
     }; 
     this.PartialViewLocationFormats = new string[2] { 
      "~/Views/{1}/{0}.cshtml", 
      "~/Views/Shared/{0}.cshtml" 
     }; 
     this.FileExtensions = new string[1] { 
      "cshtml" 
     }; 
    } 
} 

我有一點很難理解,我已經錯了所有的突然。任何建議在哪裏檢查什麼?

更新 - 代碼示例

這裏的Edit.cshtml頁的Office對象:

<div class="Section"> 
    @using (Html.BeginForm("Edit", "Offices", new { 
     id = Model.Office.Id 
    }, FormMethod.Post)) { 
     <div> 
      <input type="submit" value="Save" /> 
     </div> 
     @Html.EditorFor(m => m.Office, new { 
      Regions = Model.Regions, 
      States = Model.States 
     }) 
    } 
    @Html.Partial("Equipments", Model.Equipments) 
</div> 

這裏是爲OfficeEditorFor模板就是BEING要求:

@model Office 
<p> 
    @Html.Label("Name", "Name:") 
    @Html.TextBox("Name", Model.Name, new { 
     required = string.Empty 
    }) 
</p> 
<p> 
    @Html.Label("RegionId", "Region:") 
    @Html.DropDownList("RegionId", new SelectList((IEnumerable<Region>)ViewData["Regions"], "Id", "Name", Model.RegionId), string.Empty, new { 
     required = string.Empty 
    }) 
</p> 
@Html.EditorFor(m => m.Address) 

而這裏的OfficesController.Edit() ActionResult

[HttpGet] 
public async Task<ActionResult> Edit(
    short id) { 
    if (id > 0) { 
     Office office = await base.Repository.FindSingleOrDefaultAsync<Office, short>(id); 

     if (office != null) { 
      Task<IQueryable<Equipment>> equipments = Task.Run(() => base.Repository.FindEquipment<Office>(id)); 
      Task<IQueryable<Region>> regions = Task.Run(() => base.Repository.Find<Region>()); 
      Task<IQueryable<State>> states = Task.Run(() => base.Repository.Find<State>()); 

      await Task.WhenAll(equipments, regions, states); 

      return base.View(new OfficesView { 
       Equipments = equipments.Result.ToList(), 
       Office = office, 
       Regions = regions.Result, 
       States = states.Result, 
       ViewData = new OfficeViewData { 
        Map = new Map { 
         Center = office.Address.Position.ToPoint(), 
         Polygons = (office.Boundaries != null) ? new Polygon[] { 
          office.Boundaries.ToPolygon() 
         } : null 
        } 
       } 
      }); 
     } 
    } 

    return base.RedirectToAction("List"); 
} 

沒有生成編譯或運行時異常。 EditorFor只是靜靜地找不到模板,而是生成默認的模板。代碼模式幾乎重複其他每個對象。

+0

你是否得到任何例外,它無法找到視圖(即模板)?因爲如果你沒有得到任何例外,問題不在於你的視圖位置,而是在其他地方。 – jacqijvv

+0

不,沒有例外,而是我只是得到了對象的默認生成的編輯器。 – Gup3rSuR4c

+0

你可以發佈一個使用你的顯示和/或編輯器模板,顯示/編輯器模板名稱的表單的片段,如果你的數據模型/屬性用[[UIHint]'那。 – jacqijvv

回答

4

當查找編輯器模板時,MVC將自己將EditorTemplates/段添加到部分視圖名稱。你可以檢查源碼code here,ExecuteTemplate函數。

您所設定的局部視圖位置爲:

"~/Views/Shared/EditorTemplates/{0}.cshtml" 
"~/Views/Shared/Partials/{0}.cshtml" 

當尋找Address編輯模板,MVC將使用EditorTemplates/Address的局部視圖名稱。這意味着它會檢查2個以下局部視圖位置:

~/Views/Shared/EditorTemplates/EditorTemplates/Address.cshtml 
~/Views/Shared/Partials/EditorTemplates/Address.cshtml 

如果它不能找到他們那裏,它會回去的默認編輯模板。

可能您的編輯器模板目前位於第一個EditorTemplates文件夾中?

+0

是的,就是這樣。對於我來說,解決問題的方法是將''〜/ Views/Shared/Partials/{0} .cshtml''格式從'CSHtmlRazorViewEngine'的init中移出並直接放入類中。然後在'Global.asax'中,我只留下了一個空的init,並且它全部重新開始工作。 – Gup3rSuR4c