【问题标题】:Problem with delete operation in ASP.Net Core MVCASP.Net Core MVC 中的删除操作问题
【发布时间】:2021-03-30 21:32:14
【问题描述】:

我的应用中的删除操作有问题。

我尝试使用其他删除操作,但出现错误。

我使用的是控制器、接口,然后是视图。 也许我应该放弃接口?

这是我的ManageCategories视图:

@model collector_forum.Models.Category.CategoryIndexModel

@{
    ViewBag.Title = "Categories list";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="Lists">

    <h1 class="headersTable">List of all categories</h1>
    <div class="table-responsive-xl">
            <table class="table table-hover usersList">
                <tr style="font-size: 20px; marker:none;">
                    <th scope="col">ID</th>
                    <th scope="col">Title</th>
                    <th scope="col">Description</th>
                    <th scope="col" class="text-center align-middle">Action</th>
                </tr>
                @foreach (var category in Model.CategoryList)
                {
                    <tr style="font-size: 16px;">
                        <td class="align-middle">@category.Id</td>
                        <td class="align-middle">@category.Name</td>
                        <td class="align-middle">@category.Description</td>
                        <td class="text-center align-middle">
                            <div btn-group align-middle>
                                <form asp-action="Delete" asp-controller="Forum" asp-route-id="@category.Id" method="post">
                                    <a class="btn btn-success" asp-controller="Forum" asp-action="Edit" asp-route-id="@category.Id">Edit</a>
                                    <button type="submit" class="btn btn-danger"
                                            onclick="return confirm('Are you sure you want to delete category: @category.Name')">
                                        Delete
                                    </button>
                                </form>
                            </div>
                        </td>
                    </tr>
                }
            </table>
        </div>
</div>

此处“复制和编辑”删除操作:

public IActionResult Delete(int categoryId)
        {
            var category =  _categoryService.GetById(categoryId);

            if (category == null)
            {
                ViewBag.ErrorMessage = $"Category with ID = {categoryId} cannot be found";
                return View("NotFound");
            }
            else
            {
                var result = _categoryService.Delete(categoryId);

                if (result.IsCompleted)
                {
                    return RedirectToAction("ManageCategories");
                }

                return View("ManageCategories");
            }
        }

我在 删除 操作中引用的文件

namespace collector_forum.Data
{
    public interface ICategory
    {
        Category GetById(int id);
        IEnumerable<Category> GetAll();
        Task Create (Category category);
        Task Delete (int categoryId);
        Task UpdateCategoryTitle(int categoryId, string newTitle);
        Task UpdateCategoryDescription(int categoryId, string newDescription);
        IEnumerable<ApplicationUser> GetActiveUsers(int id);
        bool HasRecentPost(int id);
    }
}

ID 是null,我做错了什么?

请帮忙

我忘记了CategoryIndexModel

using System.Collections.Generic;

namespace collector_forum.Models.Category
{
    public class CategoryIndexModel
    {
        public IEnumerable<CategoryListingModel> CategoryList { get; set; }
    }
}

【问题讨论】:

    标签: c# asp.net-mvc asp.net-core


    【解决方案1】:

    您的表单有一个错误的categoryId 路由键,它应该是asp-route-categoryId="@category.Id"

    请注意,如果您没有使用{id} 等id 占位符定义路由模式,则此asp-route-id 会将帖子URL 呈现为参数名称为id 的查询字符串,例如?id=...。所以如果你没有定义路由模式(比如使用RouteAttribute),你可以使用FromQueryAttributecategoryId参数与id的查询值绑定,如下所示:

    public IActionResult Delete([FromQuery(Name="id")] int categoryId) { ... }
    

    或者如果你为你的id定义了一些路由模式(例如:Route("/delete/{id}")),因为参数名称不同,你需要像这样使用FromRouteAttribute

    Route("/delete/{id}")
    public IActionResult Delete([FromRoute(Name="id")] int categoryId) { ... }
    

    我认为您应该使用 asp-route- 和匹配的参数名称,这样您就不需要使用任何属性(明确指定名称)。

    【讨论】:

    • asp-route-id=" " 在其他视图中工作正常。
    • @Piotr 它不是针对视图工作,而是针对操作或处理程序。在这种情况下,由于我在回答中所说的原因,它不适用于您的 Delete 方法。如果您想保留asp-route-id,可以使用我在回答中提到的自定义属性。或者您可以将 categoryId 重命名为 id
    • @Piotr 好的,你应该接受答案然后(点击勾号),这就是 SO 的工作原理。
    猜你喜欢
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多