Aspnetcore.docs: CourseID property missing from code

Created on 2 May 2018  Â·  7Comments  Â·  Source: dotnet/AspNetCore.Docs

I think a CourseID property declaration is missing in the code where you modify Instructors/Index.cshtml.cs, in "Add courses taught by selected instructor". The code throws an error when trying to use "Model.CourseID" on Index.cshtml

Also, when you explain how selecting an Instructor works by
if(item.ID == Model.InstructorID) {selectedRow = "success";}

the code you show is from the next section:
if (item.CourseID == Model.CourseID){ selectedRow = "success";}


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Source - Docs.ms

Most helpful comment

@thomasiverson so it's bringing in the wrong code snippet. Thanks, I'll get that fixed.

All 7 comments

It's a parm in the function
public async Task OnGetAsync(int? id, int? courseID)

? means it's a nullable type. That's why it's checked before using.

    if (courseID != null)
    {
        CourseID = courseID.Value;
        Instructor.Enrollments = Instructor.Courses.Where(
            x => x.CourseID == courseID).Single().Enrollments;
    }

You use that nullable type value to set a property called "CourseID", with capital C.
if (courseID != null)
{
CourseID = courseID.Value;
//code omitted
}

There should be a property declaration such as:

     public int CourseID {get; set;}

probably after..... InstructorID {get; set;}
That way, when you can actually use it in the Instructor/Index.cshtml file:

 string selectedRow = "";
        if (item.CourseID == Model.CourseID)
        {
            selectedRow = "success";
        }

Otherwise, Model.CourseID and CourseID = courseID.value both throw errors.

CourseID is declared in the PageModel class containing that method.

if you follow the instructions exactly on this page, you will get an error due to the missing CourseID property. You can always reference the GitHub page which does include the property on line 24.
https://github.com/aspnet/Docs/blob/master/aspnetcore/data/ef-rp/intro/samples/cu/Pages/Instructors/Index.cshtml.cs

@thomasiverson so it's bringing in the wrong code snippet. Thanks, I'll get that fixed.

Thanks Rick! it appears there is an issue with the Html.ActionLink in the courses table as well (index.cshtml - line 87). it is not rendering the link properly.

Now it's running--->
1)block "@if (Model.Instructor.Courses != null){ //code }" ~ must be in 'Pages/Instructors/Index.cshtml' not in 'Pages/Courses/Index.cshtml'.
2) in 'Pages/Instructors/Index.cshtml.cs' line "CourseID = courseID.Value;" ~ must be "InstructorID = courseID.Value;"
3) same with 'Pages/Instructors/Index.cshtml' line "if (item.CourseID == Model.CourseID)" ~ must be "if (item.CourseID == Model.InstructorID)".

Was this page helpful?
0 / 5 - 0 ratings

Related issues

madelson picture madelson  Â·  3Comments

Rick-Anderson picture Rick-Anderson  Â·  3Comments

aaron-bozit picture aaron-bozit  Â·  3Comments

AnthonyMastrean picture AnthonyMastrean  Â·  3Comments

serpent5 picture serpent5  Â·  3Comments