sinanbozkus / FormHelper

ASP.NET Core - Transform server-side validations to client-side without writing any javascript code. (Compatible with Fluent Validation)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Razorpages wont redirect after POST

VictorioBerra opened this issue · comments

My razorpages code is as follows:

    [BindProperty]
    public CreateTeamViewModel CreateTeamRequest { get; set; } = default!;

    public async Task<IActionResult> OnPostAsync(CancellationToken cancellationToken)
    {
        var result = await this.validator.ValidateAsync(this.CreateTeamRequest!, cancellationToken);

        if (!result.IsValid)
        {
            result.AddToModelState(this.ModelState);
            return this.Page();
        }

        var newTeam = new Team
        {
            // .....
        };

        this.context.Teams.Add(newTeam);

        await this.context.SaveChangesAsync(cancellationToken);

        return this.RedirectToPage("/Teams/Invite", new { teamId = newTeam.Id });
    }

Chrome sees the 302:

image

My page never navigates:

image

Looks like Form Helper takes over the post, and instead makes it an AJAX call? Shouldn't it respect a 302?

So I see I should have used:

        return FormResult.CreateSuccessResult(
            "Product saved. Please wait...",
            this.Url.Page("/Teams/Invite", new { teamId = newTeam.Id }));

that works. But the README says:

You can also use the form validator without ajax.

How do I do that?

Okay after digging through samples, I found that I need [FormValidator] attribute. Turns out that it doesn't work for RazorPages:

image

What do I do now?