Search code examples
c#asp.net-corerazor-pages

Model is NULL in Display Template page when using GET in .Net Core Razor Pages


I have recently migrated to VS 2022 and .Net 7.0 (from .Net Core 3.1) and am having a problem accessing a model in the Display Template (page) using a GET. I'm not sure if this has anything to do with the migrating part, but when I POST the page the Model Value in the debugger is there, but when I use a GET, the Value is null?

Here is my stripped down code in UserNewRegisterTest.cshtml.cs:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using LimitedPlay.Areas.User.Models;

namespace LimitedPlay.Areas.User.Pages
{
    public class UserNewRegisterTestModel : PageModel
    {
        [BindProperty]
        public UserEditSP UserEditSP { get; set; }

        [BindProperty]
        public string StringTest { get; set; }


        public IActionResult OnGet()
        {
            UserEditSP UserEditSP = new UserEditSP();
            UserEditSP.NewPassword = "NewPwd";

            StringTest = "Test";

            return Page();
        }

        public IActionResult OnPost()
        {
            UserEditSP UserEditSP = new UserEditSP();
            UserEditSP.NewPassword = "NewPwd";

            StringTest = "Test";

            return Page();
        }
    }
}

And UserNewRegisterTest.cshtml:

@page "/Register/NewTest"

@model LimitedPlay.Areas.User.Pages.UserNewRegisterTestModel
@{
    ViewData["Title"] = "Register (New User)";
}

So when I browse UserNewRegisterTest.cshtml in the debugger, and look at 'this.Model' this is what shows with a GET: enter image description here

Why is the UserEditSP Model showing as null?

When I edit an existing record (where I retrieve a record from DB and POST the page from a button), everything works great, including password options set in Startup.cs (i.e. options.Password.RequireNonAlphanumeric = true;).

Also, I'm not sure if this matters, but I'm using Identity and EF Framework to resister/login users. I've created my own Register.cshtml page, and for a local account, I wanted to use the Model Validation along with JQuery validate.js and validate-unobtrusive.js. And just to be thorough, I also upgraded all NuGet packages to the latest 7.0.4 versions.

As I said, it works great when editing an existing record via POST, but when I try to use GET and UserEditSP UserEditSP = new UserEditSP(); it shows as null on the page. What am I missing here?

Any help would be greatly appreciated. Cheers... :)

P.S. I found this Razor Pages on async Get model is null and tried various combinations with the public ... OnGet(), but the model is always null.


Solution

  • In the code you have shown, you are not assigning a value to the UserEditSP property anywhere. In the OnGet and OnPost handlers, you instantiate a different instance of UserEditSP:

    UserEditSP UserEditSP = new UserEditSP();
    

    If you want to set a value to the property in the OnGet handler, you can do this:

    UserEditSP = new UserEditSP{ NewPassword = "NewPwd"};
    

    It works in the OnPost handler because the model binder instantiated the property and attempts to bind values to it from the request. If you want it to work in the same way in the OnGet handler, add SupportsGet = true to the BindProperty attribute:

    [BindProperty(SupportsGet = true]
    public UserEditSP UserEditSP { get; set; }
    

    https://www.learnrazorpages.com/razor-pages/model-binding