我试图从我的数据库中删除一个角色,但它不起作用,当我单击删除按钮时,系统崩溃并且错误消息显示如下:
找不到资源。
描述:HTTP 404。您正在查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。检查以下 URL 并确保其拼写正确。
请求的 URL:/角色/删除
这是我的 RoleController.cs 中用来删除角色的方法:
public ActionResult Delete()
{
return View();
}
public ActionResult Delete(string RoleName)
{
var thisRole = context.Roles.Where(r => r.Name.Equals(RoleName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
context.Roles.Remove(thisRole);
context.SaveChanges();
return RedirectToAction("Index");
}
我的 View 在我的“角色”文件夹中。这是使用的 View :
@{
ViewBag.Title = "Index";
}
<h2>Roles Listing </h2>
@Html.ActionLink("Create New Role", "Create") | @Html.ActionLink("Manage User Role", "ManageUserRoles")
<hr />
<div>
@foreach (var role in Model)
{
<p>
<strong>@role.Name | </strong>
<span onclick="return confirm('Are you sure to delete?')">
<a href="/Roles/Delete?RoleName=@role.Name" class="delLink" style="color:red;">Delete</a>
</span> |
@Html.ActionLink("Edit", "Edit", new { roleName = @role.Name })
</p>
}
</div>
我能做什么?
编辑(12/05/2015):
我的 RouteConfig.cs 是这样的:
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 }
);
}
}
最佳答案
错误信息表明你的路由配置不正确。
您是否为其创建了路线?
看着它,我猜这里..你的 url 实际上应该是超链接上的“角色/删除”,因为你试图通过“角色/删除”找到它可能找不到它。
没有看到你的 RouteConfig.cs 很难说。
编辑。
而不是这个:-
<a href="/Roles/Delete?RoleName=@role.Name" class="delLink" style="color:red;">Delete</a>
试试这个
<a href='@Url.Action("Delete","Role", new { rolename = @Model.Name })' class="delLink" style="color:red;">Delete</a>
关于c# - 在 asp.net mvc 5 中删除角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30154078/