Reputation: 167
I am building razor pages app using .Net Core 2.0
What is the difference between declaring a property inside the .cshtml such as follows
@functions
{
public StandardListenerViewModel listener { get; set; }
}
and the one declared in the page model
public class SingleDeviceModel : PageModel
{
[BindProperty]
public StandardListenerViewModel listener { get; set; }
public void OnGet(StandardListenerViewModel lstner)
{
this.listener = lstner;
}
}
Upvotes: 0
Views: 30
Reputation: 30065
There's no real technical difference as far as the property is concerned whether it is declared in a functions
block or a PageModel class. It still becomes a property of the generated class when the app is compiled. The difference is really one of code organisation.
Most people prefer to work with a PageModel class because it provides a clean separation between UI (markup) and request processing logic. And it is a lot easier to unit test logic. You just need to instantiate an instance of the PageModel class in your test.
Generally, functions
blocks are more likely to be used in simple demos to make the code easier to follow for proof of concepts. They probably also provide an easier ramp for those moving to Razor Pages from PHP, classic ASP or ASP.NET Web Pages where having processing logic and UI markup in the same file is a common pattern.
Upvotes: 1