【发布时间】:2013-12-13 07:16:52
【问题描述】:
我正在使用 MVC4 应用程序。我已经创建了模型类及其子集合。
由于我正在使用 .cshtml 页面开发一个强类型视图,该页面将父模型类作为其模型,在 .cshtml 页面中,我不知道如何访问子集合的属性。当我说类似的话时,
@Html.TextBoxFor(model=>model.)
它只列出了父级的直接属性,我不知道如何访问这个 .cshtml 页面中的子集合属性。
我的模特:
public partial class Parentmodel
{
public string FirstName;
public string LastName;
public ICollection<ChildEntity> ChildEntityObj { get; set; }
}
public class ChildEntity
{
public decimal PhoneNumber;
public string Addr1;
public string CountryNowLiving;
}
在视图中,我使用的是 ParentModel。因此,我无法访问 .cshtml 文件中的 PhoneNumber。不仅是电话号码,而且我在 ChildEntityCollection 中有很多字段。只有名字和姓氏出现在下拉列表中。我知道我必须使用 foreach 或 for 循环,但我不确定应该如何进行。
【问题讨论】:
-
子集合是什么意思?
-
这样的结构,public ICollection
ChildEntityObj { get;放; } 在我的父模型中。这里 ChildEntity 是具有自己属性的类。 -
你能发布你的模型类型吗?
-
谢谢@Yair。由于您的代码,我能够访问 childEntity 对象。这非常有用。
标签: c# asp.net-mvc-4 razor attributes