[英]ASP.NET MVC 5.1 EditorFor and DisplayFor not Using Custom Templates
suddenly my MVC application stopped using the custom EditorFor
or DisplayFor
templates I have. 突然,我的MVC应用程序停止使用我拥有的自定义
EditorFor
或DisplayFor
模板。 I'm not sure when exactly it failed since I've been changing the UI. 由于我一直在更改用户界面,因此我不确定它何时会失败。 I have the templates sitting in
DisplayTemplates
and EditorTemplates
under the Shared
folder. 我的模板位于
Shared
文件夹下的DisplayTemplates
和EditorTemplates
。 I do override the ViewEnginesCollection
in Global.asax
with this: 我确实用以下方法覆盖了
Global.asax
的ViewEnginesCollection
:
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new CSHtmlRazorViewEngine {
PartialViewLocationFormats = new string[] {
"~/Views/Shared/EditorTemplates/{0}.cshtml",
"~/Views/Shared/Partials/{0}.cshtml"
}
});
Where CSHtmlRazorViewEngine
is: 其中
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"
};
}
}
I'm having a bit of a hard time understanding where I've gone wrong all of the sudden. 我很难理解我突然哪里出错了。 Any recommendations on where to check for what?
关于在哪里检查什么的任何建议?
UPDATE - Code Examples UPDATE-代码示例
Here's the Edit.cshtml
page for an Office
object: 这是
Office
对象的Edit.cshtml
页面:
<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>
And here's the EditorFor
template for Office
that's being requested: 这是所请求的
Office
的EditorFor
模板:
@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)
And here's the OfficesController.Edit() ActionResult
: 这是
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");
}
There are no compilation or run-time exceptions being generated. 没有生成任何编译或运行时异常。 The
EditorFor
just silently fails to find the template and generates the default one instead. EditorFor
只是默默地找不到模板,而是生成默认模板。 The code pattern pretty much repeats for every other object. 代码模式几乎对其他每个对象都重复。
MVC will add itself the EditorTemplates/
segment to the partial view name when looking for an editor template. 当寻找编辑器模板时,MVC会将自身
EditorTemplates/
段添加到部分视图名称中。 You can check the source code here , ExecuteTemplate function. 您可以在此处查看源代码 ExecuteTemplate函数。
You have set the partial view locations as: 您已将部分视图位置设置为:
"~/Views/Shared/EditorTemplates/{0}.cshtml"
"~/Views/Shared/Partials/{0}.cshtml"
When looking for the Address
editor template, MVC will use EditorTemplates/Address
as the partial view name. 查找
Address
编辑器模板时,MVC将使用EditorTemplates/Address
作为部分视图名称。 That means it will check the 2 following partial view locations: 这意味着它将检查以下两个局部视图位置:
~/Views/Shared/EditorTemplates/EditorTemplates/Address.cshtml
~/Views/Shared/Partials/EditorTemplates/Address.cshtml
If it cannot find them there, it will go back the default editor templates. 如果无法在此处找到它们,它将返回默认编辑器模板。
Probably your editor templates are sitting currently in the first EditorTemplates folder? 可能您的编辑器模板当前位于第一个EditorTemplates文件夹中?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.