0

I created a MSSQL database with tables containing foreign keys. I then created a new MVC 4 web application. I used Entity Framework to generate my controllers/models/views. In the models, tables that were linked using foreign keys appear as ICollections. For example:

 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; }

    }
}

My question is how am I able to access the data stored in those ICollections in a view? Test.Questions[x] <-- gives errors.

4

2 に答える 2

0

ICollection<T>はまたはのようなものなIList<T>のでT[]、最初にコレクション内の要素を取得してから、そのプロパティを参照する必要があります。例えば

Test test = testService.Get(1);
Question question = test.Questions.FirstOrDefault(); // using System.Linq;
if (question.quertionType == ....)
{
}
于 2013-02-25T02:21:07.097 に答える
0

これは私が思っていたよりも簡単でした。foreachループを使用する必要がありました。

    @foreach (var question in item.Questions)
    {
        <td>@question.question1</td>
    }
于 2013-03-02T06:57:32.190 に答える