0

我有这张桌子

[Table("Quiz")]
public class Quiz
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int QuizId { get; set; }
    public string Content { get; set; }
    public string Submitby { get; set; }
    public virtual ICollection<Score> Scores { get; set; }
}

和这个

[Table("Score")]
public class Score
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ScoreId { get; set; }
    public int QuizId { get; set; }
    public string Answer { get; set; }
    public virtual Quiz Quiz { get; set; }
}

所以我有这个视图模型

public class ScoreQuizViewModel
{
    public Score Score { get; set; }
    public Quiz Quiz { get; set; }
}

并使这个控制器

    public ActionResult Details(int id = 0)
    {
        Quiz quiz = db.Quizs.Find(id);
        if (quiz == null)
        {
            return HttpNotFound();
        }
        return View(new ScoreQuizViewModel());
    }

问题是,我使用的视图上没有显示任何内容

@model SeedSimple.Models.ScoreQuizViewModel

并访问

@Html.DisplayFor(model => model.Quiz.Content)

如果我不使用 viewmodel,我可以看到结果。我该如何解决这个问题?

4

1 回答 1

2

看起来你从来没有填写你ScoreQuizViewModel的代码应该是这样的:

public ActionResult Details(int id = 0)
{
    Quiz quiz = db.Quizs.Find(id);
    if (quiz == null)
    {
        return HttpNotFound();
    }
    return View(new ScoreQuizViewModel { Quiz = quiz });
}
于 2013-06-26T16:40:16.283 回答