ASP.NET MVC:删除和更新方法不起作用(HTTP 错误 404)

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

我正在开发一个 ASP.NET MVC 项目,我在 ExperienceController 中遇到了删除和更新方法的问题。这些方法不起作用,我收到以下错误:

This localhost page isn’t working
No web page was found for the web address: https://localhost:7196/Experience/DeleteExperience/6
HTTP ERROR 404

此本地主机页面无法正常工作未找到网络页面 地址:https://localhost:7196/Experience/DeleteExperience/6 HTTP 错误 404

ExperienceController.cs:

using Microsoft.AspNetCore.Mvc;
using PortfolioNET.DAL.Context;
using PortfolioNET.DAL.Entities;

namespace PortfolioNET.Controllers
{
    public class ExperienceController : Controller
    {
        AppDbContext dbContext = new AppDbContext();
        public IActionResult ExperienceList()
        {
            var values = dbContext.Experiences.ToList();
            return View(values);
        }

        [HttpGet]
        public IActionResult CreateExperience()
        {
            return View();
        }

        [HttpPost]
        public IActionResult CreateExperience(Experience experience)
        {
            dbContext.Experiences.Add(experience);
            dbContext.SaveChanges();
            return RedirectToAction("ExperienceList");
        }

        public IActionResult DeleteExperience(int id)
        {
            var value = dbContext.Experiences.Find(id);
            dbContext.Experiences.Remove(value);
            dbContext.SaveChanges();
            return RedirectToAction("ExperienceList");
        }

        [HttpGet]
        public IActionResult UpdateExperience(int id)
        {
            var value = dbContext.Experiences.Find(id);
            return View(value);
        }

        [HttpPost]
        public IActionResult UpdateExperience(Experience experience)
        {
            dbContext.Experiences.Update(experience);
            dbContext.SaveChanges();
            return RedirectToAction("ExperienceList");
        }
    }
}


ExperienceList.cshtml:

@model List<Experience>
@{
    ViewData["Title"] = "ExperienceList";
    Layout = "~/Views/Layout/Index.cshtml";
}


<div class="content">
    <div class="container-fluid">
        <h4 class="page-title">Tables</h4>
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <div class="card-title">Striped Rows</div>
                    </div>
                    <div class="card-body">
                        <div class="card-sub">
                            Add <code class="highlighter-rouge">.table-striped</code> to rows the striped table
                        </div>
                        <table class="table table-striped mt-3">
                            <thead>
                                <tr>
                                    <th scope="col">#</th>
                                    <th scope="col">Head</th>
                                    <th scope="col">Title</th>
                                    <th scope="col">Date</th>
                                    <th scope="col">Delete</th>
                                    <th scope="col">Update</th>
                                </tr>
                            <tbody>
                                    @foreach (var item in Model)
                                    {
                                    <tr>
                                        <td>@item.ExperienceId</td>
                                        <td>@item.Head</td>
                                        <td>@item.Title</td>
                                        <td>@item.Date</td>

*                                        <td><a href="/Experience/DeleteExperience/@item.ExperienceId" class="btn btn-danger">Delete</a></td>
                                        <td><a href="/Experience/UpdateExperience/@item.ExperienceId" class="btn btn-warning">Update</a></td>*
                                    </tr>
                                    }

                            </tbody>
                        </table>
                        <a href="/Experience/CreateExperience/" class="btn btn-info btn-lg">Create a New Experience</a>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


我认为斜体部分有问题。

ID 路径是正确的,我确认它们与数据库中的现有数据匹配,并且在 ExperienceController 中正确实现了 DeleteExperience 和 UpdateExperience 方法。我已经检查了我的路线配置并确保所有内容都与我的控制器和视图中的匹配,但是问题依然存在。

c# html .net asp.net-mvc razor-pages
1个回答
0
投票

根据上面的注释,路由可能无法配置为理解

id
参数。 所以看起来它可能期待这样的 URL:

/Experience/DeleteExperience?id=6

解决此问题的一个简单方法是首先不要手动构建 URL。 让框架来做这件事。 只需使用 HTML 帮助程序即可生成链接。

所以代替这个:

<td><a href="/Experience/DeleteExperience/@item.ExperienceId" class="btn btn-danger">Delete</a></td>

类似这样的:

<td>@Html.ActionLink("Delete", "DeleteExperience", "Experience", new { id = @item.ExperienceId }, new { @class = "btn btn-danger" })</td>
© www.soinside.com 2019 - 2024. All rights reserved.