How to rewrite MVC Actions & properties to ASP Core MVVM Razor Pages

756 views Asked by At

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.

  1. 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?
  2. 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");
    }
}
1

There are 1 answers

3
Ms01 On BEST ANSWER

Razor pages is from my understanding pretty much a replacement for the old asp.net forms where you simply have a page with logic. A bit like how php does things.

If you create a page, let's say Pages/Index2.cshtml you should also create (or it may be created for you in Visual Studio) a "code-behind" file called Pages/Index2.cshtml.cs for example.

// The page file

@page
@using RazorPages
@model IndexModel2

<h2>Separate page model</h2>
<p>
    @Model.Message
</p>


// The code-behind file

using Microsoft.AspNetCore.Mvc.RazorPages;
using System;

namespace RazorPages
{
    public class IndexModel2 : PageModel
    {
        public string Message { get; private set; } = "PageModel in C#";

        public void OnGet()
        {
            Message += $" Server time is { DateTime.Now }";
        }
    }
}

You can still have models and initialize them in the code-behind file. But if you want controllers, I would suggest you do not use razor pages and just use the classical mvc. You can create a new project with it, just don't choose razor pages from the template. You certainly do not need to create a razor pages project. It is just an option. I personally do not really use it since I think is prone for one to repeat code since every code-behind file is just valid for one page afaik.

What is the PageModel?

A pagemodel is simply the code-behind file that does server side logic for your specific page.

I am not sure exactly what you are asking for, you bind models like any other razor page and properties in the code-behind class. The model is the code-behind file in my example.

how does the action/handler know which properties were passed to it from the View/Razor Page?

The action handler knows it by you specifying it in the razor page: <input asp-for="Customer.Name" />

Please read more about Razor Pages here: https://learn.microsoft.com/en-us/aspnet/core/mvc/razor-pages/?tabs=visual-studio