【发布时间】: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