我被困在試圖弄清楚爲什麼我的模板沒有被呈現或找到(因爲模板上的斷點未被命中)。我有一個模型是這樣的:ASP.NET MVC未找到/使用自定義類型編輯器模板
public class SomeModel
{
public Dropdown Cities { get; set; }
}
在該視圖
@model Mvc.Models.SomeNamespace.SomeModel
@{
Layout = "../Shared/_Site.cshtml";
}
@Html.ValidationSummary(true)
@using (Html.BeginForm())
{
@Html.EditorForModel()
<input type="submit" value="Continue">
}
在下拉對象定義
public class Dropdown
{
public int SelectedValue { get; set; }
public IEnumerable<SelectListItem> Values { get; set; }
public string Placeholder { get; set; }
}
我下創建一個編輯模板使用下拉Views/Shared/EditorTemplates/Dropdown.cshtml
@model Mvc.ViewModels.Dropdown
@Html.DropDownListFor(model => model.SelectedValue, Model.Values, Model.Placeholder)
對我來說令人震驚的是,我在該路徑下還有DateTime.cshtml模板,工作得很好。
除了Dropdown類型的屬性,模型中的每個屬性都被渲染,即使是帶有自定義模板的DateTime。
我不知道什麼?
編輯:在Cities屬性中已經嘗試使用[UIHint(「Dropdown」)]。
EDIT2:嘗試過重命名爲DropdownViewModel
不相關,但它需要的是'@ Html.DropDownListFor(m => m.SelectedValue,Model.Values,Model.Placeholder)' - 無需創建第二個相同的'IEnumerable'從第一個 –
@StephenMuecke謝謝,每一個快捷方式是讚賞:) –
相關:[可重複使用的業務對象的DropDownList編輯器模板](http://stackoverflow.com/q/6744432/181087) – NightOwl888