Is it possible to use DisplayFor () from the EditorFor template control

I use the EditorFor() helper to render the editing template in my view, and I would like to call DisplayFor() inside this template to display the display template.

Like this

this is inside /Shared/EditorTemplates/Client.ascx

 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<BusinessNext.Models.Ef.Client>" %> <%: Html.DisplayFor(client=>client) %> 

The DisplayFor template renders client properties. The DisplayFor template works fine when called from the outside, but it does not output anything from the EditorFor template. It seems like a call to DisplayFor() never gets into the DisplayFor pattern.

+5
asp.net-mvc-2
source share
2 answers

I am afraid that the only way is to use partial:

 <%= Html.Partial("~/Views/Home/DisplayTemplates/Client.ascx", Model) %> 
+3
source share

It can be debatable if it is a good idea to pattern complex objects, or if my approach to nested patterns is hack or not. The advantage of this is that in one template, the parent and child can have templates, and not select / use partial views.

All that aside, template views can be nested if you use a partial view as a transition between them.

The external template will have something like below where you want to place the internal template:

 Html.RenderPartial("SharedDisplayGoBetweenForFoo", item); 

A general partial view will look like this:

 @model Foo @Html.DisplayFor(a => a); 

Then the internal template will be called and will look like any other.

+1
source share

All Articles