首页 > 解决方案 > Html ActionLink 不会将我重定向到另一个控制器并查看

问题描述

我有一个HomeController用于从文本文件显示表格中的项目,并且每一行都有一个Add | Edit | Delete操作链接。我有另一个控制器CategoryController,它负责创建和编辑现有类别。有HomeController一个index.cshtml包含这些动作的链接。但是当我单击edit链接时,它必须调用CategoryController. 但后来我收到以下错误:

> Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.
> 
> Requested URL: /Home/Edit

这是我的操作链接(Index.cshtml)的部分代码:

 @using ToDoList
 @model ToDoList.ViewModels.CategoryItemViewModel
 //Other codes
    <td>
                            @Html.ActionLink("Add", "Create", new { id = @cats.categoryID }) |
                            @Html.ActionLink("Edit", "Edit", "Category", new { id = @cats.categoryID }) |
                            @Html.ActionLink("Delete", "Delete", new { id = @cats.categoryID })
                        </td>

这是我的CategoryController ActionResult Edit函数代码:

// GET: Category/Edit/5
        [Route("~/Category/Edit/{id}")]
        public ActionResult Edit(int id)
        {

            Category thisCategory = db.Cat.FirstOrDefault(category => category.categoryID == id);

            return View(thisCategory);
        }

编辑:我RouteConfig.cs的如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace ToDoList
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

我不知道为什么它不重定向到CategoryController并调用该Edit操作。

我的文件结构如下:

文件结构

编辑:我的HomeController课程如下:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ToDoList.Models;
using ToDoList.ViewModels;

namespace ToDoList.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        //Reading in both text files
        List<Category> categories = new List<Category>();
        List<Item> it = new List<Item>();
        public ActionResult Index()
        {
            //CatData means CategoryData
            string CatData = System.IO.File.ReadAllText(Server.MapPath("~/Data/Categories.txt"));
            string ItemData = System.IO.File.ReadAllText(Server.MapPath("~/Data/Item.txt"));

            foreach (string rowInItem in ItemData.Split('\n')) {

                if (!string.IsNullOrEmpty(rowInItem)) {

                    it.Add(new Item { 

                        itemName = rowInItem.Split(',')[0],
                        itemID = Convert.ToInt32(rowInItem.Split(',')[1]),
                        categoryID = Convert.ToInt32(rowInItem.Split(',')[2]),
                        check = rowInItem.Split(',')[3]
                    });
                }
            }
            foreach (string row in CatData.Split('\n')) {

                if (!string.IsNullOrEmpty(row)) {

                    categories.Add(new Category
                    {
                        categoryName = row.Split(',')[0],
                        categoryID = Convert.ToInt32(row.Split(',')[1]),


                    }) ;
                }
            }

            var categoryItemViewControler = new CategoryItemViewModel
            {
                Cat = categories,
                It = it
            };
            return View(categoryItemViewControler);
        }


    }
}

编辑:我CategoryItemViewModel是这样的:

using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Web;
using ToDoList.Models;

namespace ToDoList.ViewModels
{
    public class CategoryItemViewModel
    {
        //For Category
        public List<Category> Cat { get; set; }
        //For Items
        public List<Item> It { get; set; }
    }
}

编辑:我的Category.cs模型类如下:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Web;

namespace ToDoList.Models
{
    public class Category
    {
        [Required]
        public string categoryName { get; set; }
        public int categoryID { get; set; }
        public List<Item> items { get; set; }
    }
}

任何帮助或建议将不胜感激。

标签: c#htmlasp.net-mvcrazor

解决方案


编辑和索引在同一个文件夹中不存在,我认为你应该使用 /category/edit 作为 url


推荐阅读