簡體   English   中英

在ASP.NET MVC5模型中顯示外鍵屬性的值

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

我的應用程序中有一個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...
    }
}

我想將標准ApplicationUser的電子郵件屬性顯示為協會的所有者,如下所示:

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

但是在我看來什么也沒顯示。 絕對是空的。 但是,當我鍵入“ item dot User dot email”時,intellisense就會將其提取。

如果我嘗試

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

...然后我得到所有者的GUID(例如34dgh5-fegre3-235等)。

有人可以指出我需要獲得該物品嗎。User.Email外鍵屬性可以工作嗎?

謝謝。

......以下添加了控制器...

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

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

您能解決這個問題嗎? 我遇到了非常類似的問題,但是我的控制器的配置有所不同,因為我的問題是在詳細信息頁面上。但是,我在索引頁面上使用了此功能。 嘗試以下方法:

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

TABLENAME是您的表中存放電子郵件地址數據的任何位置

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM