【发布时间】:2012-03-13 04:26:58
【问题描述】:
我正在显示文件列表并允许用户从列表中删除。删除按钮对控制器进行 ajax 调用以运行“删除”操作。但是,永远不会调用删除操作。我收到了 AjaxOptions 中定义的确认警报。对于它的价值,我使用 WebForms 引擎进行了这项工作,然后将其移至 Razor。另外,这是我第一次使用区域。如果我直接调用 Delete 操作,它会起作用。这是路由问题吗?
下面是代码
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" }
)
这是我的注册路由
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>
【问题讨论】:
-
请从您注册路线的 Global.asax 中添加信息。
-
Delete 方法是否与渲染视图在同一个控制器中?如果没有,您将需要使用 ActionLink(AjaxHelper, String, String, String, Object, AjaxOptions) 重载来定义控制器。
-
是的,两者都在同一个控制器中。
-
您在问题中提到了“区域”,但我在代码/路线中没有看到任何表明您正在使用区域的内容。这个删除操作是在不同的区域吗?
-
没有。所有有问题的东西都在同一个区域。不知道在使用区域时我是否缺少任何东西。当它在 webforms 视图引擎中工作时,我没有使用它们并且没有问题。认为值得一提。
标签: c# asp.net asp.net-mvc-3 razor routing