Alex Rivera | Logout

@Html.DisplayNameFor for details model

Asked 2012-02-21T23:32:51.573
31

In my model I have an Entity

public class Carrier
{
public Guid CarrierId { get; set; }
public string Name { get; set; }
}

I also have a ViewModel

public class CarrierIndexViewModel
{
public IEnumerable<Carrier> Carriers { get; set; }
public PagingInfo PagingInfo { get; set; }
}

I have a strongly-typed (CarrierIndexViewModel) Index View, which is suppose to display a table of Carrier using PagingInfo.

I'm trying to use Html helper to display Carrier.Name in my table's header When I used IEnumerable as a model for this view I had @Html.DisplayFor(model => model.Name)

How can I get same result using my new CarrierIndexViewModel to display title for Carrier.Name?

Edit
Report

1 Answer

10

You can add a @using statement to your View to specify the Carrier type, then you will need to loop through your Model's Carriers property.

Your View will look something like..

@model CarrierIndexViewModel
@using Carrier

@foreach(Carrier c in Model.Carriers)
{
 @Html.DisplayFor(model => c.Name)
}
answered 2012-02-21T23:43:37.790

Your Answer