❔ For loop not implementing properly
Hi, I'm Tasha ! Any help would be appreciated :)
The problem: my fake DB isn't populating with data. It returns my error handle of "No data found" when there is clearly data.
The fake DB - pizza.cshtml.cs --> The Pizza Model
Models folder: PizzasModel.cs --> Getters and setters
Index.cshtml (The fake DB call to loop through everything)
Images are in "wwwroot/images/Pizzas"
34 Replies
Place breakpoints and see why your
if
condition is not being metOften times when I see model not being filled properly in a Razor Page it's because of some async shenanigans. An
async void
method, something async
not being await
ed, and so on
There's no async code in your examples, but perhaps it's among the code you truncated?I have no async stuffs anywhere, however let me try the breakpoint of the if statement real quick
I go Step over or step into and it goes to the "else statement" and has a thread exit:
@coroys ^
And what do you see the values of
Model
and Model.FakePizzaDb
are?I'm used to VScode, where it has the variables and garbage data if something is null (i.e Model = String XXX), but in visual studio it's an "output" terminal with "debug" as the category - am I looking in the wrong place?
There should be a table with values of all sorts of variables at the bottom of the screen when running debug
If not, try adding a watch to the variables you want to observe
$debug
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
Dumb - Had to Enable "Locals" - Thanks for that - Ill try again now
Model is Null
FakeDB doesn't exist apparently
I think I know what could be the issue...
You have
Pizza.cshtml.cs
and Index.cshtml
That's not how Razor Pages work
MVC, yes, you can name the views whatever
Razor Pages, noOh...?
The template should be named
Something.cshtml
and right next to it should be a codebehind file named Something.cshtml.cs
this will make the route /pizza
run the code from Pizza.cshtml.cs
and rener the template from Pizza.cshtml
Oh... Damn. So I should try moving the code to Pizza.cshtml?
and trying that?
And moving your Razor Pages to the
/Pages
folder
/Views
is for... views
Like /Controllers
is for controllersOh....!
Right, yeah that makes sense.
But then, whats the point of a view if I have a folder with ./Pages?
If you want to use controllers with views
There are two ways of doing SSR in an ASP.NET Core application
You can use controllers with views
Or you can use Razor Pages
Or you can use both in the same project, but you can't mix them
If you want to handle a
GET: /fruits
route with a Razor Page, it should be the only thing handling it
There should be no views nor controllers involvedOh.
Ohhh.
I've been trying to mix them
And if you want to handle a
POST: /animals
with controllers with views, you canI specifically want MVC - Model view controller
And you're using Razor Pages
So then, I should do this, as I want MVC, right?
If you want MVC, you will have to redo it
OH.
I see.
A
PizzaController
class inside of the /Controllers
folder
A PizzaView
inside of the /Views
A PizzaModel
or some such inside of the /Models
Then,
That makes so much sense now. I've mixed them, you can't do that which is why it broke a bunch. I didn't see I couldn't do that at first, but its cause its not apart of MVC lol.
I'll definitely work on converting the code to MVC stuff another day - but I appreciate the help and getting this sorted, thanks so much!
VS has useful code generators, you can tell it to just make a controller with view
And it'll create all the boilerplate for you
Files placed where they should be, named how they should be, etc
Oh damn, it does?
Is this when you go "Add New [....]"?
I believe so
Makes sense.
Thank you for the templates above - makes the examples udnerestandable
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.