[英]Html.DisplayNameFor not show data (.net mvc)
I have a Run MODEL that I submit to View. 我有一个提交给View的运行模型。
public class Run
{
public List<Car> cars{get; set;}
public Bike bike{get; set;}
}
public class Car
{
public name {get; set;}
public Model {get; set;}
}
public class Bike
{
public name {get; set;}
public Model {get; set;}
}
In my view I want to show List of car names. 在我看来,我想显示汽车名称列表。
@Html.DisplayNameFor(model => model.cars.Name)
In table header I am trying this. 在表标题中,我正在尝试这样做。
This is not working. 这是行不通的。 It says something reference missing?!
它说缺少参考??
In my View I am able to show all MODEL data. 在我的视图中,我可以显示所有的MODEL数据。 BUT I just can't show Heading text
但是我无法显示标题文字
In my view I also have 我认为我也有
@using //Namespace to model folder
What is wrong? 怎么了?
I think your Cars is empty . 我认为您的车空了。 Have you check weather the count is > 0
您是否检查天气计数> 0
@ if (Model.Cars!=null && Model.Cars.Any())
{
@foreach (var item in Model.Cars)
{
@Html.DisplayNameFor(i=> i.Name)
}
}
best practice is to have like this in your model to avoid null reference errors 最佳做法是在模型中使用这样的方法,以避免空引用错误
public class Run
{
public Run()
{
cars = new List<Car>()
}
public List<Car> cars{get; set;}
public Bike bike{get; set;}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.