ASP.NET 核心页面不会在后退按钮上重新加载页面

ASP.NET Core Page doesn't reload page on back button

我是 ASP.NET 和网络开发的新手。当我单击浏览器的后退按钮或使用 <a href='javascript:history.go(-1)'>Go Back to List</a> 时,我遇到了一些页面不想重新加载的问题。我收到以下错误:

重新提交表单?要正确显示此网页,请重新提交您之前输入的数据。通过这样做,您将重复此页面之前执行的任何操作。 刷新以重新提交加载此页面所需的数据。 ERR_CACHE_MISS

该页面是一个索引页面(例如 https://localhost:5001/Contact/Index),它加载了一个包含联系人的数据网格。我有一个 link 用于每个联系人,以显示他们 select 由以下代码片段表示的联系人的详细信息:

<a asp-controller="Contact" asp-action="View" asp-route-id="@contact.Id">
     <i class="fas fa-info-circle"></i>
</a>

页面控制器如下:

[HttpGet]
public IActionResult Index()
{
 ContactIndexViewModel contactIndexViewModel = new ContactIndexViewModel();
 var contacts = from c in _contactRepository.AllCompanyContacts(0) select c;
 contactIndexViewModel.FilterExpression = "Select a Filter ...";
 contactIndexViewModel.Filter = "A";
 contactIndexViewModel.Contacts = contacts;
 return View(contactIndexViewModel);
}

[HttpPost]
public IActionResult Index(ContactIndexViewModel contactIndexViewModel)
{
   var contacts = from c in _contactRepository.AllActiveContacts select c;
   string filter = contactIndexViewModel.Filter;
   contacts = contacts.Where(c => c.FirstName.ToUpper().StartsWith(filter))
                      .OrderBy(c => c.FirstName);
   contactIndexViewModel.Contacts = contacts;
   return View(contactIndexViewModel);
}

详细信息页面加载正常,但是当我从详细信息页面单击后退按钮 return 到列表时,出现上述错误。刷新页面重新加载它就好了。我是在做不该做的事,还是不该做的事? URL 的正确地址是:https://localhost:5001/Contact/Index

如有任何帮助,我们将不胜感激。

非常感谢。

--- 值

这里有一个demo可以生成ERR_CACHE_MISS,demo中的原因是index.cshtml里面的表格没有method="get"。当我点击"Details"的时候提交表格后,我会得到错误。

控制器:

[HttpPost]
        public async Task<IActionResult> Index(string movieGenre, string searchString)
        {
            // Use LINQ to get list of genres.
            IQueryable<string> genreQuery = from m in _context.Movie
                                            orderby m.Genre
                                            select m.Genre;

            var movies = from m in _context.Movie
                         select m;

            if (!string.IsNullOrEmpty(searchString))
            {
                movies = movies.Where(s => s.Title.Contains(searchString));
            }

            if (!string.IsNullOrEmpty(movieGenre))
            {
                movies = movies.Where(x => x.Genre == movieGenre);
            }

            var movieGenreVM = new MovieGenreViewModel
            {
                Genres = new SelectList(await genreQuery.Distinct().ToListAsync()),
                Movies = await movies.ToListAsync()
            };

            return View(movieGenreVM);
        }
        [HttpGet]
        public async Task<IActionResult> Index()
        {
            IQueryable<string> genreQuery = from m in _context.Movie
                                            orderby m.Genre
                                            select m.Genre;

            var movies = from m in _context.Movie
                         select m;

            var movieGenreVM = new MovieGenreViewModel
            {
                Genres = new SelectList(await genreQuery.Distinct().ToListAsync()),
                Movies = await movies.ToListAsync()
            };

            return View(movieGenreVM);
        }

Index.cshtml:

   <p>
    <a asp-action="Create">Create New</a>
</p>

<form asp-controller="Movies" asp-action="Index">
    <p>
        <select asp-for="MovieGenre" asp-items="Model.Genres">
            <option value="">All</option>
        </select>

        Title: <input asp-for="SearchString">
        <input type="submit" value="Filter" />
    </p>
</form>

<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Movies[0].Title)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Movies[0].ReleaseDate)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Movies[0].Genre)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Movies[0].Price)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.Movies)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Title)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ReleaseDate)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Genre)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Price)
                </td>
                <td>
                    <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
                    <a asp-action="Details" asp-route-id="@item.Id">Details</a> |
                    <a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
                </td>
            </tr>
        }
    </tbody>
</table>

Detail.cshtml:

<div>
    <h4>Movie</h4>
    <hr />
    <dl class="row">
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.Title)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.Title)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.ReleaseDate)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.ReleaseDate)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.Genre)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.Genre)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.Price)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.Price)
        </dd>
    </dl>
</div>
<div>
    <a asp-action="Edit" asp-route-id="@Model.Id">Edit</a> |
    <a asp-action="Index">Back to List</a> |
    <a href='javascript:history.go(-1)'>history.go</a>
</div>

结果:

第一种解决方法是改变表单类型获取,第二种是使用<a asp-action="Index">Back to List</a>而不是javascript:history.go(-1)

结果: