开发者

ASP MVC 3 Display List<> Name

开发者 https://www.devze.com 2023-03-14 03:50 出处:网络
i have this model public class Registro { [DisplayName(\"Provincia\")] [Required] public int ProvinciaID { get; set; }

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> rPr开发者_C百科ovincia in my detalis view?, i was thinking that maybe @Html.DisplayFor(modelItem => item.rProvincia.Name), any idea?, Thanks guys :)


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.


FIX:

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

with virtual i got it

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号