C
C#2y ago
Messiah

❔ Razor pages view

In this snippet
@foreach (var cafe in Model.CompanyCafes)
{
@await Html.PartialAsync("Cafe", cafe, new ViewDataDictionary(this.ViewData) { { "ShowImage", true } });
}
@foreach (var cafe in Model.CompanyCafes)
{
@await Html.PartialAsync("Cafe", cafe, new ViewDataDictionary(this.ViewData) { { "ShowImage", true } });
}
Where does razor pull the Model.CompanyCafes from? The MS docs regarding model binding is a bit confusing as it assumes I know a lot beforehand
15 Replies
seahood
seahood2y ago
at the top of the razor page code, there should be some @ entries, one of those should be @model SomeModel, which refers to a class to be used as the model in the razor code, Model refers to whatever class was used in the @model at the top
Messiah
MessiahOP2y ago
Oooh that makes sense. And I pass data to this @model through ViewData[] or one of those [Attributes] ?
seahood
seahood2y ago
so if you create a new razor page in visual studio, there will be a SomePage.cshtml and an associated SomePage.cshtml.cs (you might need to expand the cshtml one to see it, like you would a folder). the .cs file is the model for the razor page i believe the ViewData is slightly different, that's something that's just automatically available without having to specify the model (i think)
Messiah
MessiahOP2y ago
Yup, one is the view, the other is the viewmodel
seahood
seahood2y ago
so in your snippet, in the model behind, there'll be some List<string> CompanyCafes, and the view can then access that by using Model.CompanyCafes
Messiah
MessiahOP2y ago
What's confusing is [BindProperties] has to be assigned in the Model class, what if there are multiple object instances from the same class?
seahood
seahood2y ago
can you share the model class if possible?
Messiah
MessiahOP2y ago
https://github.com/kontent-ai/sample-app-razorpages/blob/62e2bdd1728add80a54029a2a8f653d993ad38d9/sample-app-razorpages/Pages/Cafes/Index.cshtml crazy how he can call Model.CompanyCafes when his model is @model sample_app_razorpages.Pages.Cafes.IndexModel. There is no /Models/Index.cs
seahood
seahood2y ago
this is the model it refers to https://github.com/kontent-ai/sample-app-razorpages/blob/62e2bdd1728add80a54029a2a8f653d993ad38d9/sample-app-razorpages/Pages/Cafes/Index.cshtml.cs no matter what the name of the model behind is in @model somerandommodelname, it will always be referred to after that point as Model in the view
Messiah
MessiahOP2y ago
Oh wow, simple like that. Declare the variable in the Index.cshtml.cs and the view can read it all I finally understand it, thank you so much!!
seahood
seahood2y ago
yep, and i believe that the [BindProperty] attributes is for allowing the view to set values in the model, rather than just having them as "read only" in the view (not 100% on that though)
seahood
seahood2y ago
no problem! this article might be some good further reading https://www.learnrazorpages.com/razor-pages/model-binding
Model Binding in Razor Pages | Learn Razor Pages
Model Binding in Razor Pages is the process that takes values from an HTTP requests and maps them to Razor Pages PageModel properties or handler method parameters.
Messiah
MessiahOP2y ago
thanks for the link also
seahood
seahood2y ago
no probs! good luck!
Accord
Accord2y 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