调用DisplayNameFor的IEnumerable的超载 [英] Calling IEnumerable overload of DisplayNameFor

查看:154
本文介绍了调用DisplayNameFor的IEnumerable的超载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这适用于抓住头文件(不会VALUES

  @model IEnumerable的< SomeModel>
...
<第i @ Html.DisplayNameFor(M = GT; m.SomeModelProperty)LT; /第i

这意味着,如果SomeModelProperty是:

  [显示(NAME =一个出色的头球)]
SomeModelProperty {搞定;组; }

然后,它会在标题显示一个出色的头球元素。

您会认为这是行不通的,因为该模型的IEnumerable,这不会有m.SomeModelProperty,但它的作品,因为的HtmlHelper有一个的HtmlHelper< IEnumerable的<的TModel>> 使得拉姆达的参数是的TModel ,而不是的IEnumerable<的TModel> 。由于这只是使用元数据,也没有必要对从集合的一个项目。 (虽然米智能感知。会骗你,让你认为这是一个集合)。我不知道什么时候加入这个凉爽的超载,但还是比较方便的Index.cshtml并摒弃了像 @ Html.DisplayNameFor(M = GT时髦的东西; @ Model.FirstOrDefault()SomeModelProperty )我希望避免的。

<一个href=\"http://msdn.microsoft.com/en-us/library/hh833697(v=vs.108).aspx\">http://msdn.microsoft.com/en-us/library/hh833697(v=vs.108).aspx

不过,我无法弄清楚如何得到这个当我的模式是不是IEnumerable的工作,而是包含了IEnumerable作为一个属性,如:

 公共类SomeList
{
   公开名单&LT; SomeModel&GT; SomeModels {搞定;组; }
   公众诠释页{搞定;组; }
   公众的DateTime CurrentAsOf {搞定;组; }
}

我希望是明确的泛型类型的参数,但我认为类型参数是由从该页面创建的HtmlHelper淌下发动机指定。我可以宣布一个新的HtmlHelper页面,或以某种方式显式地指定类型参数?

Index.cshtml:

  @model SomeList
//不知道如何做到这一点:
@ Html.DisplayNameFor&LT;&IEnumerable的LT; SomeModel&GT;&GT;(M = GT; m.SomeModelProperty)


解决方案

另一个类似的解决办法,如果没有行的作品甚至可以是:

  ...
@ {VAR哑= Model.FirstOrDefault(); }
    &所述; TR&GT;
        &LT;第i个
            @ Html.DisplayNameFor(型号=&GT; dummy.SomeModelProperty)
        &LT; /第i
...

This works for grabbing the headers(NOT VALUES):

@model IEnumerable<SomeModel>
...
<th>@Html.DisplayNameFor(m => m.SomeModelProperty)</th>

Which if SomeModelProperty were:

[Display(Name = "An Excellent Header")]
SomeModelProperty { get; set; }

Then it would display "An Excellent Header" in the header th element.

You would think this wouldn't work because the model is IEnumerable, which wouldn't have a m.SomeModelProperty, but it works because HtmlHelper has a HtmlHelper<IEnumerable<TModel>> such that the parameter of the lambda is TModel, not IEnumerable<TModel>. Since this just uses metadata, there is no need for an item from the collection. (Although intellisense on m. will lie to you and make you think it's a collection). I'm not sure when this cool overload was added, but is quite handy for Index.cshtml and does away with funky things like @Html.DisplayNameFor(m => @Model.FirstOrDefault().SomeModelProperty) which I want to avoid.

http://msdn.microsoft.com/en-us/library/hh833697(v=vs.108).aspx

However, I can't figure out how to get this to work when my model is not IEnumerable, but instead contains IEnumerable as a property, such as:

public class SomeList
{
   public List<SomeModel> SomeModels { get; set; }
   public int Page { get; set; }
   public DateTime CurrentAsOf { get; set; }
}

I was hoping to be explicit with the generic type parameters, but I think the type parameters are specified by the engine that trickles down from the HtmlHelper created with the page. Can I declare a new HtmlHelper in the page, or somehow specify the type parameters explicitly?

Index.cshtml:

@model SomeList
//No idea how to do this:
@Html.DisplayNameFor<IEnumerable<SomeModel>>(m => m.SomeModelProperty)

解决方案

Another similar workaround that works even if there are no rows could be:

...
@{var dummy = Model.FirstOrDefault(); }
    <tr>
        <th>
            @Html.DisplayNameFor(model => dummy.SomeModelProperty)
        </th>
...

这篇关于调用DisplayNameFor的IEnumerable的超载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆