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

心不动则不痛 提交于 2019-11-28 00:57:00

问题


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.


回答1:


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

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



回答2:


It can be debatable if it is a good idea to template complicated objects, or if my approach to nested templates is a hack or not. The advantage of this is having a single template for the parent and child can both have templates rather than having to choose/use partial views.

All that aside, templated views can be nested, if you use a partial view as an go between.

The outside template will have something like below where you want to place the inner template:

Html.RenderPartial("SharedDisplayGoBetweenForFoo", item);  

The shared partial would look like this:

@model Foo

@Html.DisplayFor(a => a);

The inner template would then be called and would look like any other.



来源:https://stackoverflow.com/questions/4845753/is-it-possible-to-use-displayfor-from-within-the-editorfor-template-control

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!