markjprice / cs11dotnet7

Repository for the Packt Publishing book titled "C# 11 and .NET 7 - Modern Cross-Platform Development Fundamentals" by Mark J. Price
566 stars 206 forks source link

Addin `Model` into the Razor page cause error #82

Closed cmkaya closed 1 year ago

cmkaya commented 1 year ago

Chapter: 13

Page Number: 573

Section Title: Adding code to a Razor Page

Step Number: 1 and 2

Problem to fix: In the code snippet, the DayName is set with the Model. But when I write the same, I get the "Cannot resolve symbol 'DayName'". On the other hand, when I remove the Model, the problem is fixed. Maybe I am wrong but as much as I know, the Model represents the entity models. However, we have not bound index.cshtml with in any entity model.

image image
markjprice commented 1 year ago

Model does not represent an entity model in a Razor Page. It represents the page's model.

When you create a Razor Page where the file is named index.cshtml then a class is defined for you named Pages_index, as you can see in the screenshot below. It will have a property named Model that is the same type. If you add properties to the @functions block, you are adding those properties to the Pages_index class. Because the class itself is the same thing as its own Model property, you can also use this or even nothing to refer to the current instance. So Model.DayName, this.DayName, and DayName all refer to the same property.

image

As to why your tool says "Cannot resolve symbol 'DayName'", I don't know. What are you using?

cmkaya commented 1 year ago

Thank you for the detailed explanation. I have limited knowledge of MVC, and it seems like I've gotten some concepts mixed up :)

I'm using Rider. Here's the structure of the program and the error I mentioned:

Screenshot 2023-08-27 at 21 12 37 Screenshot 2023-08-27 at 21 22 16

The second picture is just for showing the problem. I found the problem.

As I understand it, when I copy the index.html file into the Pages folder and then rename it as index.cshtml, it doesn't generate an index.cshtml.cs file. As a result, the Model is not recognized within the index.cshtml file.

By the way, I double-checked this. In the book, there is only a statement suggesting modifying the name of index.html. There isn't any information provided on adding a Razor page named index. I'm not familiar with Visual Studio, but in Rider, I have to explicitly add a Razor page.

image

Thank you for your help.

markjprice commented 1 year ago

First, in Chapter 13, we aren't yet using MVC, you are using Razor Pages.

Second, as explained in the book in the section that you copied, there is no need for "adding a Razor page named index" in any code editor, even Rider, because "you will now copy and change the static HTML page into a dynamic Razor Page" in three simple steps: (1) copy any HTML file into the Pages folder, (2) rename the file extension to .cshtml, and (3) add @page to the top of the file. That file is now a Razor Page.

A Razor Page does not need a code-behind file (.cshtml.cs file) because the code can be embedded in the .cshtml file in the @functions section. Later in the chapter on page 576 in the "Using code-behind files with Razor Pages" section I show how a Razor Page can optionally have a code-behind file so not having one is not why "the Model is not recognized within the index.cshtml file."

The Model is recognized by non-Rider code editors and by the .NET SDK. If you ignore the Rider "errors" and build and run the project you will see that Model works fine. The problem is a bug in Rider. It is lying to you. Compile "errors" are only a real problem if the .NET SDK compiler says they are. All software has bugs. Rider is software. It has a bug where it thinks there is a compile error when there isn't. In future, only get concerned if the .NET SDK compiler says there is a compile error, not when a code editor does.

markjprice commented 1 year ago

In the next edition, I will update the note on page 573 to say that Rider has the same bug as ReSharper: image

cmkaya commented 1 year ago

Thank you so much for the detailed explanation.