【问题标题】:ASP.NET MVC 5.1 EditorFor and DisplayFor not Using Custom TemplatesASP.NET MVC 5.1 EditorFor 和 DisplayFor 不使用自定义模板
【发布时间】:2014-02-27 06:39:26
【问题描述】:

我的 MVC 应用程序突然停止使用我拥有的自定义 EditorForDisplayFor 模板。由于我一直在更改 UI,因此我不确定它是什么时候失败的。我的模板位于 Shared 文件夹下的 DisplayTemplatesEditorTemplates 中。我确实用这个覆盖了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 页面:

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

这是正在请求的OfficeEditorFor 模板:

@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");
}

没有生成编译或运行时异常。 EditorFor 只是默默地找不到模板并生成默认模板。对于其他所有对象,代码模式几乎都是重复的。

【问题讨论】:

  • 您是否遇到任何无法找到视图(即模板)的异常?因为如果您没有遇到任何异常,则问题不在于您的视图位置,而在于其他地方。
  • 不,没有例外,相反,我只是获取对象的默认生成编辑器。
  • 您能否发布一个使用您的显示和/或编辑器模板之一的表单的 sn-p,以及显示/编辑器模板的名称,如果您的数据模型/属性被修饰,则可能[UIHint] 是其中的一个。
  • @jacqijvv,我已经用代码示例更新了我的问题。
  • 所以为了再确定一次,您的编辑器模板命名约定是 Office.cshtml 代表 Office 对象,Address.cshtml 代表 Address 对象等等?

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


【解决方案1】:

在查找编辑器模板时,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 文件夹中?

【讨论】:

  • 是的,就是这样。当我将"~/Views/Shared/Partials/{0}.cshtml" 格式从CSHtmlRazorViewEngine 的初始化中移出并直接进入课堂时,为我解决了这个问题。然后在Global.asax,我只是把它留给一个空的init,然后一切又开始工作了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多