foreach 循环 ASP.Net 核心 MVC 的问题

问题描述

Previous post

参考上面的前一篇文章

foreach 循环或 Action ManageCategories 是否有问题?

页面正常加载,但是去掉category后,无法通过foreach循环

查看:

@model collector_forum.Models.Category.CategoryIndexModel

@{
    ViewData["Title"] = "Categories";
}

<div class="container body-content">
    <div class="row sectionHeader">
        <div class="sectionheading">browse Categories</div>
        <div class="sectionDescription">
            <p>Welcome to <strong>Collectors Forum community</strong>. Posts are categorized by their theme</p>
            <p>
                Please read the Forum Guidelines before creating a new post.
                @if (Context.User.Identity.IsAuthenticated)
                {
                    <span>
                        You must be a
                        <a asp-area="Identity" asp-page="/Account/Register">registered member</a>
                        to create a new post
                    </span>
                }
            </p>
        </div>
    </div>
    <div  class="row" id="categoryIndexContent">
        <table class="table table-hover" id="categoryIndexTable">
            <tbody>
                @foreach (var category in Model.CategoryList)
                {
                    <tr>
                        <td>
                            @*<div class="forumlogo" style="background-image: url(@category.ImageUrl);"></div>*@
                            <div class="categoryData">
                                <div class="categoryTitle">
                                    <a asp-controller="Forum" asp-action="Topic" asp-route-id="@category.Id">@category.Name</a>
                                </div>
                                <div class="categorySubTitle">
                                    @if (category.HasRecentPost)
                                    {
                                        <div class="hasRecentPost">Hot</div>
                                    }
                                </div>
                            </div>
                        </td>
                        <td>
                            <div class="categoryPostCount">
                                @category.NumberOfPosts Posts
                            </div>
                            <div class="categoryMemberCount">
                                @category.NumberOfUsers Users
                            </div>
                        </td>
                        <td>
                            <div class="categoryDescription">
                                @category.Description
                            </div>
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>

This is the error

这里是我的删除操作代码

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


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

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

                return View("ManageCategories");
            }
        }

出了什么问题?

ManageCategories 操作:

public IActionResult ManageCategories()
        {
            var categories = _categoryService.GetAll()
               .Select(category => new CategoryListingModel
               {
                   Id = category.Id,Name = category.Title,Description = category.Description
               });

            var model = new CategoryIndexModel
            {
                CategoryList = categories
            };

            return View(model);
        }

调试方法Delete后好像不起作用=> image

删除操作

public async Task Delete(int id)
        {
            var category = GetById(id);
            _context.Remove(category);
            await _context.SaveChangesAsync();
        }

解决方法

你可以改变

return View("ManageCategories");

return RedirectToAction("ManageCategories");