[英]Deleting roles in asp.net mvc 5
I am trying to delete a Role from my database but it doesn't work , when I click the delete button, the system crashed and the error's message show me this : 我正在尝试从数据库中删除一个Role,但是它不起作用,当我单击Delete按钮时,系统崩溃了,并且错误消息向我显示了以下内容:
It can't find the resource. 它找不到资源。
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. 说明:HTTP404。您正在寻找的资源(或其依赖项之一)可能已被删除,名称更改或暂时不可用。 Review the following URL and make sure that it is spelled correctly.
查看以下URL,并确保其拼写正确。
Requested URL: / Roles / Delete 要求的网址:/角色/删除
This is the method used in my RoleController.cs to Delete de Role: 这是我的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");
}
My view is in my "Role" folder. 我的视图在我的“角色”文件夹中。 This is the view used:
这是使用的视图:
@{
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>
What can I do? 我能做什么?
Edit (12/05/2015) : 编辑(12/05/2015):
My RouteConfig.cs is this: 我的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 }
);
}
}
use HttpPost and HttpGet attribute for your actions 使用HttpPost和HttpGet属性进行操作
[HttpGet]
public ActionResult Delete()
{
return View();
}
[HttpPost]
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");
}
The [HttpPost] attribute tells the routing engine to send any POST requests to that action method. [HttpPost]属性告诉路由引擎将任何POST请求发送到该操作方法。
The error message suggests that your routing configuration is incorrect. 该错误消息表明您的路由配置不正确。
Have you created a route for it? 您是否为此创建了路线?
Looking at it, I'm guessing here.. Your url should actually be "Role/Delete" on a your hyperlink, as your trying to find it by "Roles/Delete" it probably wont find it. 看,我猜这里。您的URL实际上应该在您的超链接上是“角色/删除”,因为您尝试通过“角色/删除”来查找它可能找不到它。
Without seeing your RouteConfig.cs its hard to say. 没有看到您的RouteConfig.cs很难说。
EDIT. 编辑。
Instead of this :- 代替这个:-
<a href="/Roles/Delete?RoleName=@role.Name" class="delLink" style="color:red;">Delete</a>
Try this 尝试这个
<a href='@Url.Action("Delete","Role", new { rolename = @Model.Name })' class="delLink" style="color:red;">Delete</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.