Asinox
Asinox

Reputation: 6865

ASP MVC 3 Display List<> Name

i have this model

public class Registro
    {
      [DisplayName("Provincia")]
      [Required]
      public int ProvinciaID { get; set; }
      public List<Provincia> rProvincia { get; set; }
    }

Now what i need to do for show the Name of List<Provincia> rProvincia in my detalis view?, i was thinking that maybe @Html.DisplayFor(modelItem => item.rProvincia.Name), any idea?, Thanks guys :)

Upvotes: 1

Views: 3280

Answers (2)

Asinox
Asinox

Reputation: 6865

FIX:

public class Registro
    {
      [DisplayName("Provincia")]
      [Required]
      public int ProvinciaID { get; set; }
      public virtual Provincia rProvincia { get; set; }
    }

with virtual i got it

Upvotes: -3

Darin Dimitrov
Darin Dimitrov

Reputation: 1038800

No need to write any foreach loops. In your main view simply:

@model AppName.Models.Registro
...   
@Html.DisplayFor(x => x.rProvincia)
...

and then inside the display template ~/Views/Shared/DisplayTemplates/Provincia.cshtml:

@model AppName.Models.Provincia
<div>
    @Html.DisplayFor(x => x.Name)
</div>

This display template will be rendered for each item in the rProvincia collection.

Upvotes: 6

Related Questions