❔ Razor pages view
In this snippet
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
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 topOooh that makes sense.
And I pass data to this @model through ViewData[] or one of those [Attributes] ?
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)Yup, one is the view, the other is the viewmodel
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
What's confusing is
[BindProperties]
has to be assigned in the Model class, what if there are multiple object instances from the same class?can you share the model class if possible?
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.csthis 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 viewOh 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!!
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)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.
thanks for the link also
no probs! good luck!
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.