0

我一直试图弄清楚为什么我的模板没有被渲染或找到(因为模板上的断点没有被命中)。我有一个这样的模型:

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

Dropdown 对象定义在哪里

public class Dropdown
{
    public int SelectedValue { get; set; }

    public IEnumerable<SelectListItem> Values { get; set; }

    public string Placeholder { get; set; }
}

我在 Views/Shared/EditorTemplates/Dropdown.cshtml 下为 Dropdown 创建了一个编辑器模板

@model Mvc.ViewModels.Dropdown

@Html.DropDownListFor(model => model.SelectedValue, Model.Values, Model.Placeholder)

令我震惊的是,我在该路径下也有 DateTime.cshtml 模板,它工作得很好。

除了 Dropdown 类型的属性之外,模型上的每个属性都被渲染,即使是带有自定义模板的 DateTime 属性。

我不知道什么吗?

编辑:已经尝试在 Cities 属性中使用 [UIHint("Dropdown")] 。

EDIT2:尝试重命名为 DropdownViewModel

4

1 回答 1

2

默认情况下,模板不会递归到嵌套的复杂对象中。如果您希望发生这种情况,您始终可以通过创建~/Shared/EditorTemplates/Object.cshtml具有以下内容的 a 来覆盖此默认行为:

@if (ViewData.TemplateInfo.TemplateDepth > 1) 
{
    @ViewData.ModelMetadata.SimpleDisplayText
} 
else 
{
    foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit && !ViewData.TemplateInfo.Visited(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>
        }
    }
}

您可以在此博客文章中阅读有关 ASP.NET MVC 中默认模板的更多信息。

于 2016-06-10T08:51:55.870 回答