In the new release of ASP CORE 2.0 last month, they introduced Razor Pages, this has me in a loop since the old controller & the model frpm MVC are missing from ASP CORE 2 Razor pages.
My understanding from this page is that we get default binding of the properties using a [BindProperty] attribute
outside the Action/Method!!!!, this is because its moved to an MVVM framework
as opposed to MVC
framework.
- Question: While trying to rewrite the traditional actions, since there are no controllers, how to move to the code to the new RazorPages MVVM framework, i.e. and where and how to bind the properties, and actions/handlers?
- Since the properties are not in the signature, how does the action/handler know which properties were passed to it from the View/Razor Page?
What is the PageModel?
public class CreateModel : PageModel // what is this pagemodel, is it new or the same old model?
{
private readonly AppDbContext _db;
public CreateModel(AppDbContext db)
{
_db = db;
}
[BindProperty]
public Customer Customer { get; set; } // why is the property outside?
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
_db.Customers.Add(Customer);
await _db.SaveChangesAsync();
return RedirectToPage("/Index");
}
}