简体   繁体   中英

Problem with delete operation in ASP.Net Core MVC

I have problem with Delete operation in my app.

I tried to use my other Delete actions, but I was getting errors.

I'm using Controller, Interface and then View. Maybe I should give up with Interface?

Here is my ManageCategories View:

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

Here "copied and edited" Delete action:

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

And the file Im referring to in Delete action

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

What am I doing wrong that ID is null ?

Please, help

I forgot CategoryIndexModel

using System.Collections.Generic;

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

Your form has a wrong route key for categoryId , it should be asp-route-categoryId="@category.Id" .

Note that if you don't define a route pattern with a placeholder for id like {id} , this asp-route-id will render the post URL as query string with a parameter name of id , like ?id=... . So if you don't define a route pattern (such as by using RouteAttribute ), you can use the FromQueryAttribute to bind the categoryId parameter with the query value of id like this:

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

Or if you define some route pattern for your id (eg: Route("/delete/{id}") ), because the parameter name is different, you need to use the FromRouteAttribute like this:

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

I think you should use asp-route- with a matched parameter name so that you don't need to use any attributes (to specify the name explicitly).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM