c# - 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; }
}
}
任何帮助或建议将不胜感激。
解决方案
编辑和索引在同一个文件夹中不存在,我认为你应该使用 /category/edit 作为 url
推荐阅读
- vue.js - 如何在 vuejs 中获取更新的 $refs
- hibernate - EntityNotfoundException - 休眠和 Springboot
- c# - RichTextBox appendtext 有时会添加额外的行?
- react-native - 加载不同场景时反应原生背景空白笔触
- javascript - JavaScript时钟重新加载到自己的页面?
- javascript - TypeError: undefined is not a function ('..._this.setState...')
- python - 如何按日期对值进行排序并按列删除重复项?
- c# - docker容器之间的HTTPS通信问题
- c# - HttpClient PostAsync 抛出异常
- c - 是否可以在 bison.yh 文件中暴露 yyin ?