[英]ICollection<> inside ICollection<> in Razor - can't retrieve the model names
I'm wondering how to resolve the problem. 我想知道如何解决该问题。 I'm developing an ASP.NET MVC app
我正在开发一个ASP.NET MVC应用程序
I have a models
我有
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; }
}
I'm passing it in my Controller
(I'm writing my model into my database and then I'm retrieving it) 我将其传递到
Controller
(将模型写入数据库,然后检索它)
public ActionResult Index()
{
return View(db.SearchersString.ToList()
}
And I have a View
with: 我有一个
View
:
@model IEnumerable<App.Models.SearcherString>
The problem is in my View, I can't display the names from Path model ( CategoryID , CategoryName )
问题出在我的View中,我无法显示Path模型的名称( CategoryID , CategoryName )
I tried to do: 我试着做:
<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>
Could someone help me with this problem? 有人可以帮我解决这个问题吗?
Edit:
编辑:
Is there a way to change the Html.DisplayName
to Html.DisplayNameFor
here? 有没有办法来改变
Html.DisplayName
到Html.DisplayNameFor
这里?
@Html.DisplayName(path.CategoryID.ToString())
You should include navigation property to your query: 您应在查询中包含导航属性:
public ActionResult Index()
{
return View(db.SearchersString.Include(c=>c.Path).ToList()
}
Your Model is incorrect. 您的模型不正确。
public SearcherString()
{
Path = new List<Path>();
}
This is not a property. 这不是财产。 This is treated as a Method, a special one.
这被视为一种方法,一种特殊的方法。 And it is a constructor.
它是一个构造函数。 That Creates a Path object That is EMPTY.
创建一个名为EMPTY的Path对象。
If you want to have a one to many relationship. 如果您想拥有一对多的关系。 You would have to use the property for this and add a property of ID.
您将必须为此使用属性,并添加ID的属性。
public int PathID {get; set;}
public virtual Path Path{get; set;}
Now you have a lazy loading property that will automatically save the ID property of the Path class into your SearchString model property PathID. 现在,您有了一个惰性加载属性,该属性将自动将Path类的ID属性保存到SearchString模型属性PathID中。
to get the PathID, you will specifically need to call the include function if you're using EntityFramework, which most likely the case. 要获取PathID,如果您使用的是EntityFramework,则特别需要调用include函数,这种情况很可能会发生。
public ActionResult Index(){
return View(db.SearchersString.Include(c=>c.Path).ToList();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.