0

I am learning ASP.NET MVC. And I have created basic page which has two tables. Below is the model file Courses.cs.

public class Courses
{
    public int ID { get; set; }
    public string CourseName { get; set; }
    public string Description { get; set; }
    public int Duration { get; set; }

    public virtual ICollection<Instructors> Instructors { get; set; }
}

public class Instructors
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int CoursesID { get; set; }
    public virtual Courses Courses { get; set; }
}

After creating the model. I added the controller and corresponding view got created automatically. Now what I expect in my view is a Courses index.cshtml with field as

  1. CourseName
  2. Description
  3. Duration

Also a Instructor index.cshtml with field as

  1. FirstName

  2. LastName

  3. CoursesID ( It is a foreign key which should be linked to ID (PK) Of Courses table

Now when I open the Instructor index.cshtml file what I see is

<th>
    @Html.DisplayNameFor(model => model.Courses.CourseName)
</th>

Why the view has something like model.Courses.CourseName? Why the scaffolding does not show the below behavior. I was expecting something like below:

<th>
    @Html.DisplayNameFor(model => model.Courses.CoursesID)
</th>

Can some one please explain the behavior?

ekad
  • 14,436
  • 26
  • 44
  • 46
tanz
  • 627
  • 1
  • 10
  • 21

1 Answers1

0

Views are only going to have access to the class that you model them against. Most likely the view you are looking at is modeled against the first class you wrote. Look at the top of your razor view and see what is there.

I suspect you'll see @model Courses or some variation.

Marco
  • 41
  • 4