简体   繁体   English

ASP.NET MVC 5空的ApplicationUser

[英]ASP.NET MVC 5 empty ApplicationUser

I have a table with [QuestionID] [Description] [User_id]. 我有一个带有[QuestionID] [Description] [User_id]的表。

public int QuestionID { get; set; }
public string Description { get; set; }
public ApplicationUser user { get; set; }

The question is stored in the database when a user sends a question using a form and all the fields are saved correctly. 当用户使用表格发送问题并且所有字段都正确保存时,问题将存储在数据库中。

I send a list from my QuestionController with all the questions to my view: 我从我的QuestionController发送一个列表,其中包含所有问题到我的视图:

return View(db.Questions.ToList());

In my view I use that list: 在我看来,我使用该列表:

@foreach (var item in Model)
{
    <tr>
        <td class="td-title">
            @Html.DisplayFor(modelItem => item.QuestionID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Description)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.user.id)
        </td>
   </tr>
...

Everything works except the item.user.id which shows no value (it is filled in the database). 除了未显示任何值的item.user.id(已填入数据库)之外,其他所有内容均正常。 How come this is empty? 为什么这是空的? And what can I do to show the user information? 我该怎么做才能显示用户信息?

Edit: 编辑:

I use code-first and this is how I fill in the database: 我使用代码优先,这是我填写数据库的方式:

public ActionResult Create([Bind(Include = "QuestionID,Description")] Question question)
{
    if (ModelState.IsValid)
    {
        question.user = UserManager.FindById(User.Identity.GetUserId());

        db.Questions.Add(question);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(question);
}

Example of database result after form: 表格后的数据库结果示例:

[QuestionID]  [Description]  [user_Id]
      1          blalbla..    397d27d...

Did you try "public virtual ApplicationUser user { get; set; }" ? 您是否尝试过“公共虚拟 ApplicationUser用户{get; set;}”?

I think you have to make it virtual to access ApplicationUser properties or change the code in your view to "@Html.DisplayFor(modelItem => item.user)" 我认为您必须使其具有虚拟性才能访问ApplicationUser属性或将视图中的代码更改为“ @ Html.DisplayFor(modelItem => item.user)”

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

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