简体   繁体   English

在ASP.NET MVC5模型中显示外键属性的值

[英]Display values for a foreign key property in a model asp.net mvc5

I have a Society model in my application as such that uses a standard ApplicationUser object as it's "owner" as below: 我的应用程序中有一个Society模型,它使用标准的ApplicationUser对象作为其“所有者”,如下所示:

public class Society
{

    public int id { get; set; }

    [DataType(DataType.Text)]
    [DisplayName("Society Name")]
    [Required(ErrorMessage = "Society Name is required")]
    [StringLength(50, ErrorMessage = "Maximum of 100 characters")]
    public String societyName { get; set; }

    public String owner { get; set; }

    [DataType(DataType.DateTime)]
    public DateTime dateCreated { get; set; }

    [DataType(DataType.DateTime)]
    public DateTime dateLastUpdated { get; set; }

    [DataType(DataType.Text)]
    [DisplayName("Where is your Society based?")]
    [Required(ErrorMessage = "Location is required")]
    [StringLength(50, ErrorMessage = "Maximum of 100 characters")]
    public String location { get; set; }

    public virtual ApplicationUser User { get; set; }


    public int countMembers() {
       // to come...
    }

    public void transferOwnershipTo() {
        // to come...
    }
}

I want to display the email property of the standard ApplicationUser as the owner of the society in my view as below: 我想将标准ApplicationUser的电子邮件属性显示为协会的所有者,如下所示:

<td>
   @Html.DisplayFor(modelItem => item.User.Email)
</td>

However is displaying nothing in my view. 但是在我看来什么也没显示。 Absolutely empty. 绝对是空的。 However, the intellisense is picking it up as I type "item dot User dot email". 但是,当我键入“ item dot User dot email”时,intellisense就会将其提取。

If I try 如果我尝试

<td>
   @Html.DisplayFor(modelItem => item.owner)
</td>

... then I get the guid (eg 34dgh5-fegre3-235 etc) of the owner. ...然后我得到所有者的GUID(例如34dgh5-fegre3-235等)。

Can someone point out what I need to get the item.User.Email foreign key property to work please? 有人可以指出我需要获得该物品吗。User.Email外键属性可以工作吗?

Thanks. 谢谢。

...... Controller added below ...... ......以下添加了控制器...

public class SocietiesController : Controller
{
    private ApplicationDbContext db = new ApplicationDbContext();

    // GET: Societies
    public ActionResult Index()
    {
        return View(db.Societies.ToList());
    }

Were you able to resolve this? 您能解决这个问题吗? I'm having a very similar problem, but my controller is configured a bit differently, as my issue is for the details page... However, I have this working on my index page. 我遇到了非常类似的问题,但是我的控制器的配置有所不同,因为我的问题是在详细信息页面上。但是,我在索引页面上使用了此功能。 try the following: 尝试以下方法:

    public ActionResult Index()
    {
         var societylist = db.Societies.Include(x => x.TABLENAME);
         return View(societylist.ToList());
    }

where TABLENAME is whatever your table the email address data is housed in TABLENAME是您的表中存放电子邮件地址数据的任何位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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