我创建了一个MSSQL数据库,其中包含包含外键的表。然后我创建了一个新的MVC4Web应用程序。我使用实体框架来生成我的控制器/模型/视图。在模型中,使用外键链接的表显示为ICollections。例如:
public partial class Test
{
public Test()
{
this.Questions = new HashSet<Question>();
this.Users = new HashSet<User>();
}
public int testId { get; set; }
public string name { get; set; }
public string description { get; set; }
public Nullable<int> pointValue { get; set; }
public Nullable<int> numberOfQuestions { get; set; }
public virtual ICollection<Question> Questions { get; set; }
public virtual ICollection<User> Users { get; set; }
}
}我的问题是,如何在视图中访问存储在这些ICollections中的数据?Test.Questionsx <--给出错误。
发布于 2013-03-02 14:57:32
这比我想象的要简单得多。我只需要使用foreach循环:
@foreach (var question in item.Questions)
{
<td>@question.question1</td>
}发布于 2013-02-25 10:21:07
ICollection<T>就像一个IList<T>或T[],所以你必须首先在集合中获取一个元素,然后引用它的属性。例如:
Test test = testService.Get(1);
Question question = test.Questions.FirstOrDefault(); // using System.Linq;
if (question.quertionType == ....)
{
}https://stackoverflow.com/questions/15059086
复制相似问题