Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using EditorFor() helper to render edit template in my view and I would like to call the DisplayFor() inside this template to render out the Display template.

Like this

this is inside the /Shared/EditorTemplates/Client.ascx

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

In the DisplayFor template I render out client's properties. DisplayFor template works perfectly fine when called from everywhere else but from EditorFor template it doesn't render out anything. It seems that the DisplayFor() call never actually gets to the DisplayFor template.

like image 203
mare Avatar asked Jan 30 '11 21:01

mare


People also ask

What does HTML DisplayFor do?

DisplayFor() The DisplayFor() helper method is a strongly typed extension method. It generates a html string for the model object property specified using a lambda expression.

What is MVC editor template?

An EditorTemplate is a Razor file placed in the EditorTemplates folder: For Razor Pages apps, in the Pages/Shared/EditorTemplates folder. For MVC apps, in the Views/Shared/EditorTemplates folder or the Views/ControllerName/EditorTemplates folder.

What is templated HTML helpers in MVC?

The Html. EditorFor templated helper also renders validation logic that enforces constraints that are built into the data model or that you can add as System. ComponentModel. DataAnnotations attributes to a partial class that is associated with the data model.


1 Answers

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

<%= Html.Partial("~/Views/Home/DisplayTemplates/Client.ascx", Model) %>
like image 164
Darin Dimitrov Avatar answered Dec 31 '22 18:12

Darin Dimitrov