Can I use LabelFor() when the page inherits from IEnumerable<T>?

被刻印的时光 ゝ 提交于 2019-12-12 01:39:18

问题


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; }
}

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

IEnumerable<T>

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

Thanks for helping


回答1:


@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.




回答2:


Try this -

@Html.LabelFor(model => model.**First()**.LastName)


来源:https://stackoverflow.com/questions/4041385/can-i-use-labelfor-when-the-page-inherits-from-ienumerablet

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