如何在不更新URL的情况下使用Ajax.ActionLink

时间:2013-05-09 14:40:42

标签: c# .net ajax asp.net-mvc-4 razor

我在cshtml页面中获得了以下Ajax Actionlink:

@Ajax.ActionLink("Sort By Date",
                "Specific",
                "Events",
                new AjaxOptions {
                    UpdateTargetId="EventListContainer",
                    InsertionMode=InsertionMode.InsertAfter,
                    HttpMethod="GET"
                })

我在控制器中得到了这两种方法:

public ActionResult Overview(string user)
{
    // return PartialView here
}

public PartialViewResult Specific()
{
    // return PartialView here
}

使用以下路线:

routes.MapRoute(
  name: "EventsRoute",
  url: "Events/{user}",
  defaults: new { controller = "Events", action = "Overview" }
);

现在,每当我调用Ajax方法时,都会调用Overview,而Specific传入内部,而不是特定的方法。如何在不更新url的情况下确保调用Specific()?

2 个答案:

答案 0 :(得分:1)

像这样的东西。玩弄订购。

routes.MapRoute(
  name: "EventsSpecificRoute",
  url: "Events/Specific",
  defaults: new { controller = "Events", action = "Specific" }
);

答案 1 :(得分:0)

创建新路线

routes.MapRoute(
  name: "EventSpecific",
  url: "Events/{user}",
  defaults: new { controller = "Events", action = "Specific" }
);

当您使用自定义路线时,我建议您RouteLink而不是ActionLink

@Ajax.RouteLink("Sort By Date","EventSpecific", new AjaxOptions {  
                    UpdateTargetId="EventListContainer",
                    InsertionMode=InsertionMode.InsertAfter,
                    HttpMethod="GET"}, null)