当页面继承自IEnumerable< T>时,我可以使用LabelFor()吗? [英] Can I use LabelFor() when the page inherits from IEnumerable<T>?

查看:78
本文介绍了当页面继承自IEnumerable< T>时,我可以使用LabelFor()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有以下类定义:

Let's say, I've the following class definition:

public class Person
{
  [DisplayName("First Name")]
  public string FirstName { get; set; }
  [DisplayName("Last Name")]
  public string LastName { get; set; }
}

而且,我想使用 LabelFor(x => x.FirstName),依此类推.不幸的是,页面继承自

And, I'd like to use LabelFor(x =>x.FirstName), and so on. Unfortunately, the page inherits from

IEnumerable<T>

因此无法使用lambda表达式.有工作区吗?还是我必须使用 Label(String)版本?

so there's no way to use lambda expression. Is there any workarround? Or, I do have to use the Label(String) version?

感谢您的帮助

推荐答案

@Html.LabelFor(model => model.FirstOrDefault().LastName)

这将允许您处理列表为空时的情况...非常适合列标题.您只需要检查列表为空的情况.

This will allow you to handle when the list is empty... very suitable for column headers. All you should check is for the cases when the list is null.

这篇关于当页面继承自IEnumerable&lt; T&gt;时,我可以使用LabelFor()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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