我们在通过伙伴关系的浏览器回馈时,会遇到形式数据的问题。 NET Core Razor pagess Project. 这是你如何复制:
- Create a new ASP.NET Core Razor Pages project with this CLI command:
dotnet new webapp -n SelectIssueRepro
- Replace the content of the file
Index.cshtml
with this code:
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<form>
<select asp-for="Year" asp-items="Model.Years" onchange="this.form.submit()" class="form-select">
</select>
</form>
<p>
@Model.Message
</p>
- Replace the content of the file
Index.cshtml.cs
with this code:
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
namespace SelectIssue.Pages;
public class IndexModel : PageModel
{
[BindProperty]
public string Message { get; set; } = default!;
public SelectList Years { get; set; } = default!;
[BindProperty(SupportsGet = true)]
public int Year { get; set; } = DateTime.Today.Year;
public IActionResult OnGet()
{
var firstYear = 2017;
var years = Enumerable
.Range(firstYear, DateTime.Today.Year - firstYear + 1)
.Select(x => new SelectListItem
{
Value = x.ToString(),
Text = x.ToString(),
Selected = x == Year // Set selected year based on the Year property
})
.Reverse()
.ToList();
Message = $"You selected year {Year}";
Years = new SelectList(years, "Value", "Text", Year);
return Page();
}
}
- Build and run the project
- Select the year 2023 from the dropdown. Now the page reloads and shows:
You selected year 2023
- Select the year 2022 from the dropdown. Now the page reloads and shows:
You selected year 2022
- Now navigate back using the browsers history back button. Now the page reloads and shows:
You selected year 2023
(which is expected) but the dropdown still shows2022
(which is not expected, we expect it to show2023
). The same happens for every page while navigating back the history. The value in the dropdown always lags one year behind of what it should show. What are we doing wrong here?