0

我有这个控制器:

public class StandingsController : Controller
{
    public ViewResult Index(int id)
    {

    }

    [HttpPost]
    public JsonResult GetStage(int stage, int leagueid)
    {

    }
}

阿贾克斯调用:

 $.ajax({
            url: '@Url.Action("GetStage", "Standings")',
            type: 'POST',
            data: { stage: currentStage, leagueid:leagueid },
            success: function (data) {
                    .............

我在页面加载后发出 ajax 请求。我想要的只是将 url 从更改http://localhost/MyApp/Standings/Index/3http://localhost/MyApp/Standings/3. 我添加了一条自定义路线,如下所示:

 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 routes.RouteExistingFiles = false;
            routes.MapRoute(null, "Standings/{id}", new { controller = "Standings", action = "Index" });
 routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });

但现在我在进行 ajax 调用时出现异常:

   The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ViewResult Index(Int32)' in 'MyApp.Controllers.StandingsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

我不明白 ajax 调用与 ViewResult 中的 id 参数有什么关系。我真的需要一些帮助。谢谢。

4

1 回答 1

1

它期待一个ID。试试这个:

url: '@Url.Action("GetStage", "Standings", new { id = 1 })',

...或这个:

routes.MapRoute(null, "Standings/{id}", 
    new { controller = "Standings", action = "Index" }
    new { id = UrlParameter.Optional, });
于 2012-06-23T21:46:07.450 回答