简体   繁体   中英

Html.DisplayNameFor not show data (.net mvc)

I have a Run MODEL that I submit to 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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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