Ray
Ray

Reputation: 65

asp.net mvc ViewModel result wont appear

i have this table

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

and this

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

so i have this viewmodel

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

and make this controller

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

the problem is, theres nothing shown on my view im using

@model SeedSimple.Models.ScoreQuizViewModel

and accessing with

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

i can see the result if im not using viewmodel. how i can fix this?

Upvotes: 0

Views: 55

Answers (1)

Mgetz
Mgetz

Reputation: 5128

It appears you're never filling in your ScoreQuizViewModel your code should look like this:

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

Upvotes: 2

Related Questions