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.
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.
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.
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.
I am afraid that the only way is to use a partial:
<%= Html.Partial("~/Views/Home/DisplayTemplates/Client.ascx", Model) %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With