【问题标题】:Calling the home controller action using ajax使用 ajax 调用 home 控制器动作
【发布时间】:2011-08-24 21:11:43
【问题描述】:

我的家庭控制器中有一个“索引”操作,它响应 url '/'。该操作还有一个可选的“id”参数...

[HttpGet]
public ViewResultBase Index(int id = -1)
{
     ....
}

这个路由是...

routes.MapRoute("Home",
                "",
                 new { controller = "Home", action = "Index", id = -1 } );

如果我尝试使用 ajax 调用此操作...

    $.get(
        '@Url.Action("Index", "Home")',
        { id: 20 },
        function (response) {
        });

调用失败,因为 url 是 /?id=20

是否可以强制 Url.Action 包含控制器和动作名称,就像我这样做时...

'@Url.Action("Index", "Home")' + 'Home/Index'

一切正常,还是我需要更正我的路由?

【问题讨论】:

    标签: asp.net-mvc-3


    【解决方案1】:

    你不需要像这样为你的 Action 方法创建一个特殊的路由。默认路由:

     {controller}/{action}/{id}, 
     new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    

    已经处理了这种情况;当您在服务器上请求“/”时,您应该被发送到您的 Home/Index 操作方法。您应该删除您指定的自定义路由,并让默认路由将您发送到正确的目的地。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-22
      • 1970-01-01
      相关资源
      最近更新 更多