Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将ApplicationUser的用户名获取到视图中_C#_Entity Framework_Asp.net Mvc 5 - Fatal编程技术网

C# 将ApplicationUser的用户名获取到视图中

C# 将ApplicationUser的用户名获取到视图中,c#,entity-framework,asp.net-mvc-5,C#,Entity Framework,Asp.net Mvc 5,我试图显示问题作者的姓名,但与模型的其他属性不同,ApplicationUser.UserName属性不会显示在视图中(它只是空白)。我正在使用实体框架、MVC5和Razor 我的问题模型: public class Question { public int QuestionID { get; set; } public string Title { get; set; } public ApplicationUser Author { get; set; }

我试图显示问题作者的姓名,但与模型的其他属性不同,ApplicationUser.UserName属性不会显示在视图中(它只是空白)。我正在使用实体框架、MVC5和Razor

我的问题模型:

public class Question
{
    public int QuestionID { get; set; }
    public string Title { get; set; }
    public ApplicationUser Author { get; set; }
    public string Description { get; set; }
    [DisplayName("Created")]
    public DateTime CreationDateTime { get; set; }
    public int Rating { get; set; }
}
我的问题控制器索引操作:

// GET: Questions
    public ActionResult Index()
    {
        return View(db.Questions.ToList());
    }
以及以下观点:

@model IEnumerable<Waegogi.Models.Question>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Title)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Author.UserName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Description)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CreationDateTime)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Rating)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Author.UserName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Description)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CreationDateTime)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Rating)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.QuestionID }) |
            @Html.ActionLink("Details", "Details", new { id=item.QuestionID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.QuestionID })
        </td>
    </tr>
}

</table>
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
指数

@ActionLink(“新建”、“创建”)

@DisplayNameFor(model=>model.Title) @DisplayNameFor(model=>model.Author.UserName) @DisplayNameFor(model=>model.Description) @DisplayNameFor(model=>model.CreationDateTime) @DisplayNameFor(model=>model.Rating) @foreach(模型中的var项目){ @DisplayFor(modeleItem=>item.Title) @DisplayFor(modelItem=>item.Author.UserName) @DisplayFor(modelItem=>item.Description) @DisplayFor(modelItem=>item.CreationDateTime) @DisplayFor(modelItem=>item.Rating) @ActionLink(“编辑”,“编辑”,新的{id=item.QuestionID})| @ActionLink(“详细信息”,“详细信息”,新的{id=item.QuestionID})| @ActionLink(“删除”,“删除”,新的{id=item.QuestionID}) }

我哪里出错了?

您应该能够通过在控制器调用中显式加载关系来解决这个问题:

public ActionResult Index()
{
     return View(db.Questions.Include(q => q.ApplicationUser).ToList());
}

您可能需要应用程序用户导入?还有那个类的实例?你们是通过像nHibernate这样的ORM来获取问题的吗?可能是延迟加载相关属性。我正在使用实体框架,我对它几乎没有经验。这解决了我的问题。问题是我对延迟加载的无知。