ASP.NET核心页在后退按钮时不能重载页面。

问题描述 投票:0回答:1

我是一个ASP.NET和Web开发的新手。 我遇到了一些问题,当我点击浏览器的后退按钮或使用 <a href='javascript:history.go(-1)'>Go Back to List</a>. 我收到以下错误信息。

重新提交表格? 要正确显示这个网页,请重新提交你之前输入的数据。这样做,你会重复这个页面之前执行的任何操作.刷新以重新提交加载这个页面所需的数据.ERR_CACHE_MISS。

该页是一个索引页(例如 https:/localhost:5001ContactIndex)。)来加载一个包含联系人的Datagrid。我为每个联系人设置了一个链接,以显示他们所选择的联系人的详细信息,下面是代码片段。

<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);
}

详情页加载正常,但是当我点击返回按钮从详情页返回列表时,我得到了上面描述的错误。刷新页面后重新加载就好了。是我做了不该做的事还是没做该做的事?URL的地址是正确的。 https:/localhost:5001ContactIndex。

如果有任何帮助,将非常感激。

谢谢你的帮助。

---Val

asp.net-core page-refresh
1个回答
0
投票

这里是一个演示可以产生 ERR_CACHE_MISS而在演示中,原因是形式上的 index.cshtml 没有method="get".当我提交表格后,我点击 "详细信息 "时,我会得到错误信息。

Controller.Index.cshtml:错误。

[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>

结果。enter image description here

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

结果:enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.