Razor 中的 ICollection<> 中的 ICollection<> - 无法检索模型名称
ICollection<> inside ICollection<> in Razor - can't retrieve the model names
我想知道如何解决这个问题。我正在开发 ASP.NET MVC 应用程序
我有一个models
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
中传递它(我正在将我的模型写入我的数据库,然后我正在检索它)
public ActionResult Index()
{
return View(db.SearchersString.ToList()
}
我有一个 View
有:
@model IEnumerable<App.Models.SearcherString>
The problem is in my View, I can't display the names from Path model
(CategoryID, CategoryName)
我试过:
<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>
但我只有:
有人可以帮我解决这个问题吗?
Edit:
这里有办法把Html.DisplayName
改成Html.DisplayNameFor
吗?
@Html.DisplayName(path.CategoryID.ToString())
您应该在查询中包含导航 属性:
public ActionResult Index()
{
return View(db.SearchersString.Include(c=>c.Path).ToList()
}
您的模型不正确。
public SearcherString()
{
Path = new List<Path>();
}
这不是 属性。这被视为一种方法,一种特殊的方法。它是一个构造函数。这创建了一个 EMPTY 的 Path 对象。
如果你想建立一对多的关系。您必须为此使用 属性 并添加 ID 的 属性。
public int PathID {get; set;}
public virtual Path Path{get; set;}
现在您有一个延迟加载 属性,它会自动将路径 class 的 ID 属性 保存到您的 SearchString 模型 属性 PathID 中。
要获取 PathID,如果您使用 EntityFramework,您将特别需要调用 include 函数,这很可能是这种情况。
public ActionResult Index(){
return View(db.SearchersString.Include(c=>c.Path).ToList();
}
我想知道如何解决这个问题。我正在开发 ASP.NET MVC 应用程序
我有一个models
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
中传递它(我正在将我的模型写入我的数据库,然后我正在检索它)
public ActionResult Index()
{
return View(db.SearchersString.ToList()
}
我有一个 View
有:
@model IEnumerable<App.Models.SearcherString>
The problem is in my View, I can't display the names from Path model (CategoryID, CategoryName)
我试过:
<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>
但我只有:
有人可以帮我解决这个问题吗?
Edit:
这里有办法把Html.DisplayName
改成Html.DisplayNameFor
吗?
@Html.DisplayName(path.CategoryID.ToString())
您应该在查询中包含导航 属性:
public ActionResult Index()
{
return View(db.SearchersString.Include(c=>c.Path).ToList()
}
您的模型不正确。
public SearcherString()
{
Path = new List<Path>();
}
这不是 属性。这被视为一种方法,一种特殊的方法。它是一个构造函数。这创建了一个 EMPTY 的 Path 对象。
如果你想建立一对多的关系。您必须为此使用 属性 并添加 ID 的 属性。
public int PathID {get; set;}
public virtual Path Path{get; set;}
现在您有一个延迟加载 属性,它会自动将路径 class 的 ID 属性 保存到您的 SearchString 模型 属性 PathID 中。
要获取 PathID,如果您使用 EntityFramework,您将特别需要调用 include 函数,这很可能是这种情况。
public ActionResult Index(){
return View(db.SearchersString.Include(c=>c.Path).ToList();
}