ASP.NET Core Pages Handler

Unknown

My login and register forms are on the same page.

In ASP.NET Core razor pages, I have a page called account, in which there are two handlers, one is OnPostLogin for login and OnPostRegister for new user registration.

I defined two properties at the class level, loginModel and registerModel, so that I can connect the form inputs to each property with tag helper. In the asp-page-handler, I put each form for its own handler. When I want to do validation in the code, use this code.

if (ModelState.IsValid)

It is never valid and is always invalid because of other form entries, that is, the login entries are not valid for registration (because they are empty) and the register form entries are not valid for login either.

How do I solve the problem?

I bet

if (ModelState.IsValid)
{
    // Register Or Login Action 
}

I deleted it from my code and performed validation on the client side with jQuery validation, but I have to do this on the server side as well.

Mike Brind

You can separate the login and register forms into two different partial views. This way, each form will have its own isolated scope and validation context.

Here's my working demo based on this PageModel:

public class RegisterOrLoginModel : PageModel
{
    public class RegisterModel
    {
        [Required]
        public string UserName { get; set; }
        [Required]
        public string Password { get; set; }
    }
    public class LoginModel
    {
        [Required]
        public string UserName { get; set; }
        [Required]
        public string Password { get; set; }
    }
}

Here's are the partials showing how to pass the relevant class to its @model directive:

_RegisterPartial.cshtml

@model RegisterOrLoginModel.RegisterModel
<h4>Register</h4>
<form method="post" asp-page-handler="Register">
    <input asp-for="UserName" /><br />
    <input asp-for="Password" /><br />
    <button>Submit</button>
</form>

_LoginPartial.cshtml

@model RegisterOrLoginModel.LoginModel
<h4>Log In</h4>
<form method="post" asp-page-handler="Login">
    <input asp-for="UserName" /><br />
    <input asp-for="Password" /><br />
    <button>Submit</button>
</form>

Include both partial in the Razor page like this:

<partial name="_LoginPartial" model="new RegisterOrLoginModel.LoginModel()" />
<partial name="_RegisterPartial" model="new RegisterOrLoginModel.RegisterModel()" />

Then, instead of having two bound properties in your PageModel, update your handlers to take the respective model as a parameter. Then ModelState will only work with the parameter and will not be affected by other stuff:

public void OnPostRegister(RegisterModel model)
{
    if(ModelState.IsValid)
    {
        // ...
    }
}
public void OnPostLogin(LoginModel model)
{
    if (ModelState.IsValid)
    {
        // ...
    }
}

Alternatively, create separate pages for each form.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ASP.NET Core Razor Pages - How to bind an asp-page-handler to a checkbox without Javascript

Setting Kendo UI Grid DataSource Read property with Handler in ASP.NET Core MVC with Razor Pages

ASP.Net Core 3.1 Razor Pages event handler not working in production

Is there a way to catch a HTTP request to a non existing custom handler for Razor Pages in ASP.NET Core 2.1?

partial page model on post handler is not executing | ASP.NET Core Razor Pages 2.0

Asp .Net Core policy handler not triggering

Http handler in asp.net core

Partial Pages in ASP.NET Core 2.0

Authorization in ASP .NET Core Razor pages

ASP.NET Core Pages: Partials

Rename Pages/Shared directory in ASP.NET Core Razor Pages

ASP.NET Core Razor pages vs Full MVC Core

Authorization failing for custom authentication handler for ASP.NET Core 3.1?

Pass an exception to the default ASP.Net CORE error handler

ASP.NET Core map route to static file handler

ASP.NET Core Authorization Policies: Can't step into the handler?

ASP.NET Core: Razor: Post QueryString as is to a razor handler

How to create generic route handler in ASP.NET Core

the program is not able to find handler for MediatR query ASP.Net Core

ASP.NET Core Razor - Global Exception Handler

How to redirect on ASP.Net Core Razor Pages

Bootstrap Alerts in Asp.net Core Razor Pages

ASP.NET Core Razor Pages button is not working

Implement Sorting With ASP.NET Core Razor Pages

ASP net Core Razor Pages - split models for EF and views

Specify language for ASP.NET Core 2 Localizer in Razor Pages

ASP.NET Core Razor Pages Routing With parameters

Using a DI with partial views in asp.net core razor pages

Asp .Net Core 2.2 Razor Pages Ajax Call Post not working