ASP.NET MVC 5.1 EditorFor和DisplayFor不使用自定义模板

时间:2014-02-27 06:39:26

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我的MVC应用程序突然停止使用我拥有的自定义EditorForDisplayFor模板。因为我一直在改变UI,所以我不确定它到底是什么时候失败了。我的模板位于DisplayTemplates文件夹下的EditorTemplatesShared。我确实使用以下内容覆盖ViewEnginesCollection中的Global.asax

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>

以下是EditorFor的{​​{1}}模板请求:

Office

这是@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"); } 只是默默无法找到模板并生成默认模板。每个其他对象都会重复代码模式。

1 个答案:

答案 0 :(得分: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文件夹中吗?