English 中文(简体)
在协会中通过浏览器进行反馈时,发布表格数据。 NET Core Razor Pages Project
原标题:Issue with form data when navigating back via browser in a ASP.NET Core Razor Pages project

我们在通过伙伴关系的浏览器回馈时,会遇到形式数据的问题。 NET Core Razor pagess Project. 这是你如何复制:

  1. Create a new ASP.NET Core Razor Pages project with this CLI command: dotnet new webapp -n SelectIssueRepro
  2. 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>
  1. 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();
    }
}
  1. Build and run the project
  2. Select the year 2023 from the dropdown. Now the page reloads and shows: You selected year 2023
  3. Select the year 2022 from the dropdown. Now the page reloads and shows: You selected year 2022
  4. 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 shows 2022 (which is not expected, we expect it to show 2023). 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?
问题回答

autocomplete=“off>>>,其形式为:将确定问题。

<form autocomplete="off">
    <select asp-for="Year" asp-items="Model.Years" onchange="this.form.submit()" class="form-select">
    </select>
</form>

<>试验<>

“entergraph





相关问题
.NET 7 minimal API request timeout

I found this in .NET 8 (prerelease) but I can t use it due to company policy. https://learn.microsoft.com/en-us/aspnet/core/performance/timeouts?view=aspnetcore-8.0&viewFallbackFrom=aspnetcore-7.0 ...

Blazor web assembly pass checkbox list values to model

I m new to Blazor. I m working in a web assembly Blazor project. I am trying to create a form that passes values back to a model. I have it working with input text fields and drop downs, but I am ...

asp.net 6 c# mvc one to many get data from many side

I am trying to access the data on the many side of a one to many relationship. In the Index action of my controller I assign the data to a variable and return it. I have a Blog, the one side, with ...

How to update shared view adminheader.cshtml from a model

I ve added a drop down into my shared adminheader.cshtl view and it needs to be dynamically updated from data from a table. So when I use another view like routeplan.cshtml I load the view from ...

Gitlab CI On Prem, Docker Image and ASP.NET Core 7

We have a .NET 6 application. We added CI using: image: mcr.microsoft.com/dotnet/sdk:6.0 before_script: - dotnet restore --packages $NUGET_PACKAGES_DIRECTORY build: stage: build script: - ...

ASP.NET Core 2 .1 - How to show all data in table

I don t know how to put CustomerData into CustomerLists property. I m already using CustomerLists = CustomerData; but I got error: CustomerLists is a type but is used like a variable Can anyone ...

热门标签