Skip to content

asp-page-handler (formaction) missing from button (Razor Pages) #12005

@Icidis

Description

@Icidis

Describe the bug

FormAction missing when optional route-values are not provided when using ASP.NET Core Razor Pages.

To Reproduce

Steps to reproduce the behavior:

  1. Using this version of ASP.NET Core 2.2
  2. Create a page as shown below
@page "{schoolId?}/{handler?}"
@model RazorPages.RouteData.Pages.Students.NewModel
@{
    ViewData["Title"] = "New Student";
}

<h1>New Student</h1>

<form method="post">
    <input asp-for="Student.Id" type="hidden" />
    <div class="form-group row">
        <label asp-for="Student.SchoolId" class="col-sm-2 col-form-label">School</label>
        <div class="col-sm-10">
            <select class="form-control" asp-for="Student.SchoolId" asp-items="Model.Schools">
                <option value="">Pick one</option>
            </select>
            <span asp-validation-for="Student.SchoolId" class="form-text m-b-none"></span>
        </div>
    </div>
    <div class="form-group row">
        <label asp-for="Student.FirstName" class="col-sm-2 col-form-label">First Name</label>
        <div class="col-sm-10">
            <input class="form-control" required asp-for="Student.FirstName" />
            <span asp-validation-for="Student.FirstName" class="form-text m-b-none"></span>
        </div>
    </div>
    <div class="form-group row">
        <label asp-for="Student.LastName" class="col-sm-2 col-form-label">last Name</label>
        <div class="col-sm-10">
            <input class="form-control" required asp-for="Student.LastName" />
            <span asp-validation-for="Student.LastName" class="form-text m-b-none"></span>
        </div>
    </div>
    <hr />
    <div class="form-group row">
        <div class="col-sm-10 col-sm-offset-2">
            <div class="button-box">
                <button class="btn btn-primary btn-sm" type="submit" asp-page-handler="SaveStaged"><i class="fa fa-save"></i> Save for Later</button>
                <button class="btn btn-success btn-sm" type="submit" asp-page-handler="ValidateAndSave"><i class="fa fa-save"></i> Valiate and Save</button>
            </div>
        </div>
    </div>
</form>
  1. Add the 2 post methods to the PageModel
        public void OnGet(int? schoolId)
        {
            Student = new Student(
                id: 0,
                firstName: string.Empty,
                lastName: string.Empty,
                schoolId: schoolId.GetValueOrDefault(0));
        }

        public IActionResult OnPostSaveStaged()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            _dataRepository.CreateStudent(student: Student);

            return RedirectToPage("/Students/Index");
        }

        public IActionResult OnPostValidateAndSave()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            _dataRepository.CreateStudent(student: Student);

            return RedirectToPage("/Students/Index");
        }
  1. Navigate to page with populate schoolId value and inspect both buttons on page. Notice the formaction values are populated.
E.G. https://localhost:44304/Students/New/1
  1. Navigate to the page against but without setting the schoolId value and inspect the buttons again. You will notice that the formaction values are now missing.

Expected behavior

formaction values are correctly populated.

Screenshots

Route-Data-Given

Route-Data-Not-Given

Additional context

If I change the @page hander from

@page "{schoolId?}/{handler?}"

to

@page "{schoolId?}"

the formaction is set correctly but I would prefer not showing the handler as a querystring, I've even tried swapping the {schoolId?} and {handler?} around but then the route-value-schoolId does not get populated in OnGet.

DotNet Info
PS C:\DevTest\RazorPages.RouteData> dotnet --info
.NET Core SDK (reflecting any global.json):
Version: 3.0.100-preview5-011568
Commit: b487ff10aa

Runtime Environment:
OS Name: Windows
OS Version: 10.0.17134
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\3.0.100-preview5-011568\

Host (useful for support):
Version: 3.0.0-preview5-27626-15
Commit: 61f30f5a23

.NET Core SDKs installed:
1.0.3 [C:\Program Files\dotnet\sdk]
2.1.202 [C:\Program Files\dotnet\sdk]
2.1.402 [C:\Program Files\dotnet\sdk]
2.1.403 [C:\Program Files\dotnet\sdk]
2.1.502 [C:\Program Files\dotnet\sdk]
2.1.503 [C:\Program Files\dotnet\sdk]
2.1.507 [C:\Program Files\dotnet\sdk]
2.1.700 [C:\Program Files\dotnet\sdk]
2.2.107 [C:\Program Files\dotnet\sdk]
2.2.300 [C:\Program Files\dotnet\sdk]
3.0.100-preview5-011568 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.0-preview5-19227-01 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 1.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 1.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.0.0-preview5-27626-15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.0.0-preview5-27626-15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesquestion

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions