C
C#16mo ago
Ripunzill

❔ 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
using Microsoft.AspNetCore.Mvc.RazorPages;
using Assignment_2.Models;

namespace Assignment_2.Views
{
public class PizzaModel : PageModel
{
public List<PizzasModel> fakePizzaDB = new List<PizzasModel>()
{
new PizzasModel(){ImageTitle="Margerita", PizzaName="Margerita", BasePrice=10, TomatoPizzaSauce=true, Cheese=true, Pepperoni=false, Ham=false, Capsicum=false, Mushroom=false},
new PizzasModel(){ImageTitle="Pepperoni", PizzaName="Pepperoni", BasePrice=10, TomatoPizzaSauce=true, Cheese=true, Pepperoni=true},
};

public void OnGet()
{
}
}
}
using Microsoft.AspNetCore.Mvc.RazorPages;
using Assignment_2.Models;

namespace Assignment_2.Views
{
public class PizzaModel : PageModel
{
public List<PizzasModel> fakePizzaDB = new List<PizzasModel>()
{
new PizzasModel(){ImageTitle="Margerita", PizzaName="Margerita", BasePrice=10, TomatoPizzaSauce=true, Cheese=true, Pepperoni=false, Ham=false, Capsicum=false, Mushroom=false},
new PizzasModel(){ImageTitle="Pepperoni", PizzaName="Pepperoni", BasePrice=10, TomatoPizzaSauce=true, Cheese=true, Pepperoni=true},
};

public void OnGet()
{
}
}
}
Models folder: PizzasModel.cs --> Getters and setters
namespace Assignment_2.Models
{
public class PizzasModel
{
public string ImageTitle { get; set; }
public string PizzaName { get; set; }
public bool Cheese { get; set; }
public float FinalPrice { get; set; }
}
}
namespace Assignment_2.Models
{
public class PizzasModel
{
public string ImageTitle { get; set; }
public string PizzaName { get; set; }
public bool Cheese { get; set; }
public float FinalPrice { get; set; }
}
}
Index.cshtml (The fake DB call to loop through everything)
@model Assignment_2.Views.PizzaModel // Pizza.cshtml.cs

@if (Model != null && Model.fakePizzaDB != null && Model.fakePizzaDB.Count > 0)
{
@foreach (var pizza in Model.fakePizzaDB)
{

var ImagePath = "~/images/Pizzas/" + (pizza.ImageTitle + ".png");

<div class="">

<img src="@ImagePath" asp-append-version="true" />
<a asp-area="" asp-page="" asp-route-ImageTitle="@pizza.ImageTitle"
asp-route-PizzaPrice="@pizza.FinalPrice">Get this pizza! </a>
<br />

</div>
}
}
else
{
<p>No pizza data available.</p>
}
@model Assignment_2.Views.PizzaModel // Pizza.cshtml.cs

@if (Model != null && Model.fakePizzaDB != null && Model.fakePizzaDB.Count > 0)
{
@foreach (var pizza in Model.fakePizzaDB)
{

var ImagePath = "~/images/Pizzas/" + (pizza.ImageTitle + ".png");

<div class="">

<img src="@ImagePath" asp-append-version="true" />
<a asp-area="" asp-page="" asp-route-ImageTitle="@pizza.ImageTitle"
asp-route-PizzaPrice="@pizza.FinalPrice">Get this pizza! </a>
<br />

</div>
}
}
else
{
<p>No pizza data available.</p>
}
Images are in "wwwroot/images/Pizzas"
34 Replies
CoRoys
CoRoys16mo ago
Place breakpoints and see why your if condition is not being met
Angius
Angius16mo ago
Often 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 awaited, and so on There's no async code in your examples, but perhaps it's among the code you truncated?
Ripunzill
RipunzillOP16mo ago
I have no async stuffs anywhere, however let me try the breakpoint of the if statement real quick
Ripunzill
RipunzillOP16mo ago
I go Step over or step into and it goes to the "else statement" and has a thread exit:
The thread 0x8030 has exited with code 0 (0x0).

The thread 0x7e6c has exited with code 0 (0x0).
The thread 0x8030 has exited with code 0 (0x0).

The thread 0x7e6c has exited with code 0 (0x0).
Ripunzill
RipunzillOP16mo ago
@coroys ^
Angius
Angius16mo ago
And what do you see the values of Model and Model.FakePizzaDb are?
Ripunzill
RipunzillOP16mo ago
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?
Angius
Angius16mo ago
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
MODiX
MODiX16mo ago
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.
Ripunzill
RipunzillOP16mo ago
Dumb - Had to Enable "Locals" - Thanks for that - Ill try again now
Ripunzill
RipunzillOP16mo ago
Ripunzill
RipunzillOP16mo ago
Model is Null FakeDB doesn't exist apparently
Angius
Angius16mo ago
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, no
Ripunzill
RipunzillOP16mo ago
Oh...?
Angius
Angius16mo ago
The template should be named Something.cshtml and right next to it should be a codebehind file named Something.cshtml.cs
\root
|— Pages
| |— Pizza.cshtml
| |— Pizza.cshtml.cs
\root
|— Pages
| |— Pizza.cshtml
| |— Pizza.cshtml.cs
this will make the route /pizza run the code from Pizza.cshtml.cs and rener the template from Pizza.cshtml
Ripunzill
RipunzillOP16mo ago
Oh... Damn. So I should try moving the code to Pizza.cshtml? and trying that?
Angius
Angius16mo ago
And moving your Razor Pages to the /Pages folder /Views is for... views Like /Controllers is for controllers
Ripunzill
RipunzillOP16mo ago
Oh....! Right, yeah that makes sense. But then, whats the point of a view if I have a folder with ./Pages?
Angius
Angius16mo ago
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 involved
Ripunzill
RipunzillOP16mo ago
Oh. Ohhh. I've been trying to mix them
Angius
Angius16mo ago
And if you want to handle a POST: /animals with controllers with views, you can
Ripunzill
RipunzillOP16mo ago
I specifically want MVC - Model view controller
Angius
Angius16mo ago
And you're using Razor Pages
Ripunzill
RipunzillOP16mo ago
So then, I should do this, as I want MVC, right?
Angius
Angius16mo ago
Angius
Angius16mo ago
If you want MVC, you will have to redo it
Ripunzill
RipunzillOP16mo ago
OH. I see.
Angius
Angius16mo ago
A PizzaController class inside of the /Controllers folder A PizzaView inside of the /Views A PizzaModel or some such inside of the /Models Then,
public class PizzaModel
{
public List<Pizza> Pizzas = ...
}
public class PizzaModel
{
public List<Pizza> Pizzas = ...
}
[Route("[controller]")]
public class PizzaController : Controller
{
[HttpGet]
public IActionResult GetPizzas()
{
var model = new PizzaModel();
return View(model);
}
}
[Route("[controller]")]
public class PizzaController : Controller
{
[HttpGet]
public IActionResult GetPizzas()
{
var model = new PizzaModel();
return View(model);
}
}
@model Models.PizzaModel

@foreach (var pizza in Model.Pizzas)
{
<span>@pizza.Name</span> — <strong>@pizza.Price</strong>
}
@model Models.PizzaModel

@foreach (var pizza in Model.Pizzas)
{
<span>@pizza.Name</span> — <strong>@pizza.Price</strong>
}
Ripunzill
RipunzillOP16mo ago
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!
Angius
Angius16mo ago
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
Ripunzill
RipunzillOP16mo ago
Oh damn, it does? Is this when you go "Add New [....]"?
Angius
Angius16mo ago
I believe so
Ripunzill
RipunzillOP16mo ago
Makes sense. Thank you for the templates above - makes the examples udnerestandable
Accord
Accord16mo ago
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.
Want results from more Discord servers?
Add your server