如何显示MVC索引页的导航属性值的集合 [英] How to display values from Navigation Property collections in MVC Index page

查看:114
本文介绍了如何显示MVC索引页的导航属性值的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为JOBTITLE'的EF实体类型有一个名为导航属性办公室(这是一个名为办公室的另一个实体类型的集合)。

I have an EF Entity Type named 'JobTitle' which has a Navigation Property named 'Offices'(which is a collection of another Entity Type named 'Office').

我想在我的索引视图中显示JOBTITLE名称和相关局名称的列表。
查看脚手架默认生成以下内容:

I would like to display a list of JobTitle names and related Office names in my Index view. The View scaffolding has generated the following by default:

@model IEnumerable<Example.Models.JobTitle>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.JobTitleName)
        </th>

    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.JobTitleName)
        </td>

    </tr>
}

</table>

我的控制器code(默认)是这样的:

My controller code (by default) looks like:

    // GET: /JobTitle/

    public ActionResult Index()
    {
        return View(db.JobTitles.ToList());
    }

这将让我产生所有属于JOBTITLE(如JobTitleName')简单的属性。不过,我可以不知道如何来检索'办公室'集合(导航属性)对应的办公室的信息到我的看法。

This will allow me to generate all of the simple properties that belong to 'JobTitle' (such as 'JobTitleName'). However I cannot work out how to retrieve the corresponding 'Office' information from the 'Offices' collection (Navigation Property) into my view.

所以,我希望能有两列,JOBTITLE和办公室。的显示落得

So, I am hoping to end up with a display of two columns, 'JobTitle' and 'Office'.

(在我的数据库中的这些实体被重新由三个表psented $ P $:JOBTITLE(JobTitleID,JobTitleName),办公室(OfficeID,OfficeName)和JobTitleOffice(JobTitleID,OfficeID))

(In my database these entities are represented by three tables: JobTitle (JobTitleID,JobTitleName), Office (OfficeID,OfficeName) and JobTitleOffice (JobTitleID,OfficeID))

我是完全新的MVC和Entity Framework,所以任何帮助感激地接受。

I am completely new to MVC and Entity Framework, so any help gratefully received.

谢谢!

推荐答案

在未来,像这样的问题,如果它您发布您的实际实体类是最好的。然而,根据您的表,它看起来像你有 JOBTITLE 办公室 A多对一对多的关系。你的困难是最有可能涉及到如何访问 OfficeName 属性特定 JOBTITLE 实例,问题有那有不只是一个 OfficeName ,但许多。您需要遍历办公室秒。

In the future, with questions like this, it's best if you post your actual entity classes. However, based on your tables, it looks like you have a many-to-many relationship between JobTitle and Office. Your difficulty is most likely related to how to access the OfficeName property for a particular JobTitle instance, and the problem there is that there's not just one OfficeName, but many. You need to iterate over the list of Offices.

<td>
    @Html.DisplayFor(modelItem => item.JobTitleName)
    @foreach (var office in item.Offices)
    {
        @Html.DisplayFor(m => office.OfficeName)
    }
</td>

如果你想有一个稍微好一点的显示器,您可能希望打开办公室的名称到一个逗号分隔的列表(例如,办事处名称1,办公室名称2,... ),它可以通过执行以下操作,而不是使用foreach来实现:

If you want a slightly better display, you might want to turn the names of the offices into a comma-delimited list (e.g., Office Name 1, Office Name 2, ...) which can be achieved by doing the following instead of using the foreach:

<td>
    @Html.DisplayFor(modelItem => item.JobTitleName)
    @string.Join(", ", item.Offices.Select(m => m.OfficeName))
</td>

或者,如果你只关心呈现一个办公室的名称,你可以使用 FirstOrDefault 拉出来一个办公室:

<td>
    @Html.DisplayFor(modelItem => item.JobTitleName)
    @{
        var office = item.Offices.FirstOrDefault();
        if (office != null)
        {
            @Html.DisplayFor(m => office.OfficeName);
        }
    }
</td>

您应经常检查第一,以防万一居然没有相关的办公室。

You should always check for null first, just in case there's actually no related offices.

基本上,你选择哪种方法只是取决于应用程序的需求。此外,要注意实体框架的相关项目的延迟加载。这不是那么糟糕,因为你第一次尝试访问办公室,一个新的查询会发出来获取相关处室。因为,你不尝试访问任何相关的属性关办公室,即一个额外的查询将不会影响太大。但考虑到以下几点:

Basically, which method you choose simply depends on your application's requirements. Also, pay attention to Entity Framework's lazy loading of related items. Here it's not so bad, as the first time you try to access Offices, a single new query will be issued to fetch the related offices. Since, you're not trying to access any related properties off of Office, that one extra query won't affect much. But consider the following:

public class Office
{
    ...

    public virtual Address Address { get; set; }
}

如果你试图做这样的事情:

If you tried to do something like:

@foreach (var office in item.Offices)
{
    @office.OfficeName
    <address>
        @office.Address.Street
        ...
    </address>
}

您想最终什么所谓的N + 1查询,在这里你有一个查询来获取集合(办公室),然后一个查询来获取地址每个的办公室(导致 N 查询,其中ñ 是办公室的总数)。在这种情况,你就要去渴望加载好。例如,

You'd end up with what's called an N+1 query, where you have one query to fetch the collection (Offices) and then one query to fetch the Address for each office (resulting in N queries, where N is the total number of offices). In this type of situation, you're better off eager-loading. For example,

return View(db.JobTitles.Include("Offices.Address").ToList());

这会导致实体框架做了一个连接以包括办公室,每个地址每个办公室实例的所有查询来获取职称。那么你可以做你想要的任何这些信息不管,而不必担心被颁发多个其他查询。

That would cause Entity Framework to do a join to include both Offices and each Address for each Office instance all in the query to fetch the job titles. You could then do whatever you wanted with any of this information without worrying about multiple additional queries being issued.

这篇关于如何显示MVC索引页的导航属性值的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆