Joe
Joe

Reputation: 2107

Why are the properties of my view model not displaying?

I'm using the MVC 4 Beta but this doesn't appear to be specific to MVC 4. I'm new to MVC so this is probably just me needing to be pointed in the right direction. Thanks!

I'm using a view model defined as...

public class PatientWithVisits
{
    public Patient Patient { get; set; }

    public IEnumerable<Visit> Visits { get; set; }

}

In my view, I'm using the HTML helper (LabelFor, in this case) and passing my properties via lambda expressions, like this @Html.LabelFor(model => model.VisitId).

I started digging and quickly I realized my mistake. The view model doesn't have a VisitId property. However, fixing the problem didn't come as easy as finding the problem.

I tried changing model => model.VisitId to model => model.Visits.VisitId but that didn't work. The intellisense for model includes Patient and Visits as I expected. However, Visits did not contain any of the properties I was expecting to see for a Visit, like VisitId. The intellisense for the Visits collection contained only extension methods.

What is going on and how do I get the properties of my Visit to display?

Upvotes: 0

Views: 123

Answers (2)

David Spence
David Spence

Reputation: 8079

Visits is a collection, but each individual Visit in the collection will have a VisitId. To simply display them as requested:

@foreach (var visit in Model.Visits)
{
    @visit.VisitId
}

Upvotes: 1

TheGeekYouNeed
TheGeekYouNeed

Reputation: 7539

You also need to add a property to your ViewModel called ViewId

Upvotes: 0

Related Questions