ASP.NET MVC 5.1 EditorFor和Display不使用自定义模板
突然我的MVC应用程序停止使用我自己的自定义EditorFor
或DisplayFor
模板。 因为我一直在改变UI,所以我不确定它到底是什么时候失败了。 我将模板放在Shared
文件夹下的DisplayTemplates
和EditorTemplates
。 我用这个覆盖Global.asax
的ViewEnginesCollection
:
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" }; } }
我有点难以理解我突然出错的地方。 关于在哪里检查什么的任何建议?
更新 – 代码示例
这是Office
对象的Edit.cshtml
页面:
@using (Html.BeginForm("Edit", "Offices", new { id = Model.Office.Id }, FormMethod.Post)) { @Html.EditorFor(m => m.Office, new { Regions = Model.Regions, States = Model.States }) } @Html.Partial("Equipments", Model.Equipments)
这里是要求的Office
的EditorFor
模板:
@model Office @Html.Label("Name", "Name:") @Html.TextBox("Name", Model.Name, new { required = string.Empty })
@Html.Label("RegionId", "Region:") @Html.DropDownList("RegionId", new SelectList((IEnumerable)ViewData["Regions"], "Id", "Name", Model.RegionId), string.Empty, new { required = string.Empty })
@Html.EditorFor(m => m.Address)
这是OfficesController.Edit() ActionResult
:
[HttpGet] public async Task Edit( short id) { if (id > 0) { Office office = await base.Repository.FindSingleOrDefaultAsync(id); if (office != null) { Task<IQueryable> equipments = Task.Run(() => base.Repository.FindEquipment(id)); Task<IQueryable> regions = Task.Run(() => base.Repository.Find()); Task<IQueryable> states = Task.Run(() => base.Repository.Find()); 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"); }
没有生成编译或运行时exception。 EditorFor
只是默默地找不到模板并生成默认模板。 代码模式几乎为每个其他对象重复。
在查找编辑器模板时,MVC会将EditorTemplates/
segment添加到部分视图名称中。 你可以在这里查看源代码 ,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文件夹中吗?
- 任何人都可以告诉为什么在使用StreamWriter保存数据时仍然显示以前的数据
- ASP.NET machinekey在代码中设置键
- ASP.NET JavaScriptSerializer需要HttpResponse吗?
- 堆栈跟踪如何指向错误的行(“返回”语句) – 40行关闭
- 在EF Code First DatabaseIntializer中重用GUID
- 凭据标志为“true”,但是“Access-Control-Allow-Credentials”
- 如何在Asp.net中将值从一个表单传递到另一个表单
- 没有名为“ASP.NET 4.6.81.0”的EventLog源。 此模块需要.NET Framework 2.0
- 如何在没有重定向的情况下在ASP.NET中显示自定义404页面?