You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Html.EditorForModel()返回空白页,Razor未识别Object.cshtml模板

Hey, let's figure out why your Object.cshtml template isn't playing nice with Html.EditorForModel() — this is a super common gotcha with MVC's editor templates, especially the special Object template. Let's break down the fixes step by step:

1. First, double-check your template's location and naming

MVC looks for editor templates in very specific places, and case matters (even if your dev machine is Windows):

  • Your Object.cshtml must live in either ~/Views/Shared/EditorTemplates/ (for global use across all controllers) or ~/Views/[YourControllerName]/EditorTemplates/ (for controller-specific use).
  • The filename has to be exactly Object.cshtml — no typos, no extra suffixes, case-sensitive. If you named it object.cshtml (lowercase 'o'), MVC might not pick it up on case-sensitive servers (like Linux).
  • Make sure your template's model declaration is flexible enough: use @model object or @model dynamic instead of a strict type, so it can handle any complex model you pass.

2. Fix the #1 culprit: accidental recursive calls

The Object template is meant to render complex models by iterating over their properties — but if you accidentally call Html.EditorForModel() or Html.EditorFor(x => x) inside the template, you'll trigger an infinite recursion. That usually leads to a blank page (or a server timeout).

Here's a safe, working version of an Object.cshtml template that avoids this:

@model object

@if (Model != null)
{
    foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit && !ViewData.TemplateInfo.Visited(pm)))
    {
        <div class="form-group mb-3">
            @Html.Label(prop.PropertyName, new { @class = "form-label" })
            @* Use Editor() instead of EditorForModel() to target individual properties *@
            @Html.Editor(prop.PropertyName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessage(prop.PropertyName, new { @class = "text-danger small" })
        </div>
    }
}

The key here is ViewData.TemplateInfo.Visited(pm) — this tracks properties we've already rendered to avoid circular references (like a model that references itself) and recursion.

3. Verify your model and metadata settings

  • If your model is null when you call Html.EditorForModel(), and your template doesn't handle null cases, you'll get a blank page. That's why the @if (Model != null) check in the template above is important.
  • Make sure your model properties don't have attributes that hide them from scaffolding, like [ScaffoldColumn(false)] or [HiddenInput(DisplayValue = false)] — unless you intentionally want to skip them.
  • When calling Html.EditorForModel(), don't pass a templateName parameter that overrides the default Object template selection.

4. Debug if the template is even being loaded

To test if MVC is finding your template at all, add a simple debug line at the top of Object.cshtml:

<p>✅ Object template loaded!</p>

If you see this line when you run your app, the template is being picked up — which means the issue is in your template's logic (like missing property iteration or recursion). If you don't see it, go back and double-check the location/naming rules from step 1.


内容的提问来源于stack exchange,提问作者Junior

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:16:01