简体   繁体   English

在Asp.net MVC中为DisplayNameFor方法创建模板

[英]Create a Template for DisplayNameFor method in Asp.net MVC

I'm creating a Asp.net MVC Site for the first type. 我正在为第一种类型创建一个Asp.net MVC网站。

I'm creating a Details page, and used something like this: 我正在创建一个“详细信息”页面,并使用了以下内容:

<ul>
    <li>
        @Html.DisplayNameFor(m => m.MyProp)
        @Html.DisplayFor(m => m.MyProp)
    </li>
<ul>

And I get something like that: 我得到这样的东西:

<ul>
    <li>
        MyProp
        MyPropValue
    </li>
<ul>

I'd like my field header and my field value to be wrapped inside individual Span's, so i can format it in a global css, like that: 我希望将字段标题和字段值包装在各个Span中,以便可以在全局CSS中对其进行格式化,如下所示:

<ul>
    <li>
        <span class="field-label">MyProp</span>
        <span class="field-value">MyPropValue</span>
    </li>
<ul>

I'd like to find a way to do that without having to enclose every field in a span. 我想找到一种方法,而不必将范围中的每个字段都括起来。 I get to solve that for DisplayFor method using a DisplayTemplate, but know i need to do that for DisplayNameFor, in a way that every time i use this Method it generates the span automatically. 我可以使用DisplayTemplate解决DisplayFor方法的问题,但我知道我需要针对DisplayNameFor进行此操作,因此我每次使用此Method都会自动生成跨度。

I thought about creating my own substitute method for @Html.DisplayNameFor, but I'm a little confused about what would be the signature for that method. 我曾考虑过为@ Html.DisplayNameFor创建自己的替代方法,但对于该方法的签名感到有些困惑。

Anybody got any tip on how to achieve that? 任何人都知道如何实现这一点?


Edit: I tried creating a HtmlHelper extension method, like bellow: 编辑:我试图创建一个HtmlHelper扩展方法,像下面这样:

public static IHtmlString OxDisplayNameFor<TModel, TField> (this HtmlHelper<TModel> helper, Expression<Func<TModel, TField>> expression)
{
    return new HtmlString(String.Format(
        "<span class='display-label'>{0}</span>", "XXX"));
}

The only thing that remains is: How do i get the value from the expression parameter? 剩下的唯一事情是:如何从expression参数获取值?

The signature would be the same as for Html.DisplayNameFor : 签名将与Html.DisplayNameFor相同:

public static MvcHtmlString DisplayWithDisplayNameFor<TModel, TValue>(
    this HtmlHelper<TModel> html,
    Expression<Func<TModel, TValue>> expression)
{
    // construct a span tag and put html.DisplayNameFor(expression) in it
    // then another span tag and put html.DisplayFor(expression) in it
    // return all that
}

It would be more convenient to have it as a @helper , but you cannot have a generic helper and so cannot pass the property-pointing lambdas to it, so the closest you can do is something like: 将它作为@helper会更方便,但是您不能拥有泛型帮助程序 ,因此无法将指向属性的lambda传递给它,因此您可以执行的最接近的操作是:

@helper DisplayWithDisplayName(MvcHtmlString DisplayName, MvcHtmlString DisplayValue)
{
    <span class="field-label">@DisplayName</span>
    <span class="field-value">@DisplayValue</span>
}

which you would then call as 然后您将其称为

@DisplayWithDisplayNameFor(Html.DisplayNameFor(m => m.MyProp), Html.DisplayFor(m => m.MyProp))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM