c# - 在 asp.net mvc 5 中删除角色

标签 c# asp.net database asp.net-mvc-5 roles

我试图从我的数据库中删除一个角色,但它不起作用,当我单击删除按钮时,系统崩溃并且错误消息显示如下:

找不到资源。

描述: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/

相关文章:

c# - 有没有办法告诉我在运行时使用的是哪个枚举?

c# - 是否有适用于 Silverlight 的 C# 动态模拟框架?

c# - 如何在 GridView 中启用 IEnumerable 数据的自动排序?

javascript - ASP.NET Ajax 出现错误,代码为 0

sql - 使用 substr 时出现 to_date 错误

php - MySQL - 将行合并为一

c# - 我的 PDF 语法坏了!如何将图像放入PDF?

c# - Main 方法是 JIT 吗?

javascript - 关于将动态数据输入到 html 页面的表单的建议

python - Django - 数据库错误 : No such table