首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Razor中的ICollection<> in ICollection<> -无法检索模型名称

Razor中的ICollection<> in ICollection<> -无法检索模型名称
EN

Stack Overflow用户
提问于 2015-09-17 07:41:34
回答 2查看 1.4K关注 0票数 0

我在想如何解决这个问题。我正在开发一个ASP.NET MVC应用程序

我有一个models

代码语言:javascript
复制
public class SearcherString
{
    public int ID { get; set; }
    public string Description { get; set; }
    public virtual ICollection<Path> Path { get; set; 
    public SearcherString()
    {
        Path = new List<Path>();
    }
}

public class Path
{
    public int ID { get; set; }
    public int CategoryID { get; set; }
    public string CategoryName { get; set; }
}

我正在将它传递到我的Controller中(我正在将我的模型写入我的数据库,然后检索它)

代码语言:javascript
复制
public ActionResult Index()
{
    return View(db.SearchersString.ToList()
}

我有一个View,它有:

代码语言:javascript
复制
@model IEnumerable<App.Models.SearcherString>

问题是在我的视图中,我无法显示来自路径模型的名称

我试着做:

代码语言:javascript
复制
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Description)
        </th>
        @foreach (var item in Model)
        {
            foreach (var path in item.Path)
            {
                <th>
                    @Html.DisplayName(path.CategoryID.ToString())
                </th>
                <th>
                    @Html.DisplayName(path.CategoryName)
                </th>
            }
        }
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Description)
            </td>
            @foreach (var path in item.Path)
            {
                <td>
                    @Html.DisplayFor(modelItem => path.CategoryID)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => path.CategoryName)
                </td>
            }
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
                @Html.ActionLink("Details", "Details", new { id = item.ID }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.ID })
            </td>
        </tr>
    }
</table>

但我只有:

有人能帮我解决这个问题吗?

编辑:

这里有办法将Html.DisplayName更改为Html.DisplayNameFor吗?

代码语言:javascript
复制
@Html.DisplayName(path.CategoryID.ToString())
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-17 07:46:54

应该将导航属性包含到查询中:

代码语言:javascript
复制
public ActionResult Index()
{
    return View(db.SearchersString.Include(c=>c.Path).ToList()
}
票数 1
EN

Stack Overflow用户

发布于 2015-09-17 08:06:20

你的模型不正确。

代码语言:javascript
复制
public SearcherString()
{
    Path = new List<Path>();
}

这不是财产。这被视为一种方法,一种特殊的方法。它是一个构造函数。它创建一个空的Path对象。

如果你想有一对多的关系。您必须为此使用该属性并添加一个ID属性。

代码语言:javascript
复制
public int PathID {get; set;}
public virtual Path Path{get; set;}

现在有了一个延迟加载属性,它将自动将Path类的ID属性保存到SearchString模型属性PathID中。

要获得PathID,特别需要在使用EntityFramework时调用EntityFramework函数,这很有可能是这样的。

代码语言:javascript
复制
public ActionResult Index(){


  return View(db.SearchersString.Include(c=>c.Path).ToList();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32624961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档