我正在尝试从jQuery.ajax调用运行一个action方法,但我看到的是正在运行控制器名称POST方法“Edit”而不是我在url属性“DeleteItem”中提供的操作< / p>
客户端
$.ajax({
type: 'POST',
url: '/Edit/DeleteItem?id=1',
contentType: 'application/json',
success: function () {
alert('Deleted OK');
},
error: function (xhr, ajaxOptions, thrownError) {
alert(thrownError);
},
});
服务器代码
// I expect it to be running this
[HttpPost]
public ActionResult DeleteItem(string id)
{
return Json("");
}
// And not this
[HttpPost]
[Authorize]
[ValidateInput(false)]
public ActionResult Edit(ViewModel)
{
try
....
路线配置
routes.MapRoute(
"Edit", // Route name
"Edit/{id}", // URL with parameters
new { controller = "Edit", action = "Edit", id = UrlParameter.Optional });
我在JQuery.ajax方法中有什么问题吗?
由于
答案 0 :(得分:2)
根据您发布的路线配置,您的所有路线都将转到Edit
操作。您需要根据URL配置操作。试试这个:
routes.MapRoute(
"Edit", // Route name
"Edit/{action}/{id}", // URL with parameters
new { controller = "Edit", action = "Edit", id = UrlParameter.Optional });
您的行动将在以下网址提供:
// Edit:
http://localhost:63672/Edit/Edit
// Delete:
http://localhost:63672/Edit/DeleteItem