Ajax.ActionLink 不调用控制器操作
我正在显示文件列表并允许用户从列表中删除。删除按钮对控制器进行 ajax 调用以运行“删除”操作。但是,删除操作永远不会被调用。我收到 AjaxOptions 中定义的确认警报。无论如何,我使用 WebForms 引擎完成了这项工作,然后将其移至 Razor。另外,这是我第一次使用 Areas。如果我直接调用删除操作,它就会起作用。这是路由问题吗?
这是后面的代码
public EmptyResult Delete(string fileName)
{
if (fileName.IsNullOrEmpty()) return null;
var model = new Models.BenefitBookletModel();
model.DeleteBooklet(fileName);
return null;
}
这是标记
@Ajax.ActionLink("test", "Delete", new { fileName = item.FileName }, new AjaxOptions
{
Confirm = "Are you sure you want to delete " + item.FileName + "?",
OnComplete = "function(){deleteComplete('" + item.jsReferenceableFileName + "')}",
HttpMethod = "DELETE",
OnFailure = "function(){alert('Could not delete file');}"
}, new { @class = "DeleteButton" }
)
这是我的 RegisterRoutes
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("SubscriberAll","subscriber/{id}",new { controller = "Subscriber", action = "ShowAll" },new { id = @"\d+" } );
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
来自区域注册的路线
context.MapRoute("Marketing_default","Marketing/{controller}/{action}/{id}",new { action = "Index", id = UrlParameter.Optional }
这是生成的标记
<a href="/Marketing/BenefitBooklet/Delete?fileName=MyFileName.pdf" data-ajax-method="GET" data-ajax-failure="function(){alert('Could not delete file');}" data-ajax-confirm="Are you sure you want to delete MyFileName.pdf?" data-ajax-complete="function(){deleteComplete('MyFileName.pdf')}" data-ajax="true" class="DeleteButton"> </a>
I'm displaying a list of files and allowing the user to delete from the list. The delete button does an ajax call to the controller to run the 'Delete' action. However, the delete action is never called.I am getting the confirmation alert defined in AjaxOptions. For what it's worth, I had this working using the WebForms engine and just moved it to Razor. Also, this is the first time I've used Areas. If I call the Delete action directly, it works. Is this a routing issue?
Here's the code behind
public EmptyResult Delete(string fileName)
{
if (fileName.IsNullOrEmpty()) return null;
var model = new Models.BenefitBookletModel();
model.DeleteBooklet(fileName);
return null;
}
Here's the mark up
@Ajax.ActionLink("test", "Delete", new { fileName = item.FileName }, new AjaxOptions
{
Confirm = "Are you sure you want to delete " + item.FileName + "?",
OnComplete = "function(){deleteComplete('" + item.jsReferenceableFileName + "')}",
HttpMethod = "DELETE",
OnFailure = "function(){alert('Could not delete file');}"
}, new { @class = "DeleteButton" }
)
Here is my RegisterRoutes
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("SubscriberAll","subscriber/{id}",new { controller = "Subscriber", action = "ShowAll" },new { id = @"\d+" } );
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Routes from area registration
context.MapRoute("Marketing_default","Marketing/{controller}/{action}/{id}",new { action = "Index", id = UrlParameter.Optional }
Here is the markup generated
<a href="/Marketing/BenefitBooklet/Delete?fileName=MyFileName.pdf" data-ajax-method="GET" data-ajax-failure="function(){alert('Could not delete file');}" data-ajax-confirm="Are you sure you want to delete MyFileName.pdf?" data-ajax-complete="function(){deleteComplete('MyFileName.pdf')}" data-ajax="true" class="DeleteButton"> </a>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该将函数名称指定为相应 AjaxOptions 属性的值。添加
script
部分:在您的视图中并更改“AjaxOptions”中的
OnFailure
和OnComplete
:You should specify the function name as the value of the corresponding AjaxOptions property. Add
script
section:in your view and change
OnFailure
andOnComplete
in 'AjaxOptions':您将操作链接的 HttpMethod 定义为 DELETE,但您的方法可能只接受 GET。尝试用删除动作动词来修饰它。
You are defining the HttpMethod of the Action Link to be DELETE but you method probably only accepts GET. Try decorating it with the Delete action verb.