我有这个控制器:
public class StandingsController : Controller
{
public ViewResult Index(int id)
{
}
[HttpPost]
public JsonResult GetStage(int stage, int leagueid)
{
}
}
Ajax电话:
$.ajax({
url: '@Url.Action("GetStage", "Standings")',
type: 'POST',
data: { stage: currentStage, leagueid:leagueid },
success: function (data) {
.............
我在页面加载后发出ajax请求。
我想要的是将网址从http://localhost/MyApp/Standings/Index/3
更改为http://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参数。 我真的需要一些帮助。感谢。
答案 0 :(得分:1)
期待一个id。试试这个:
url: '@Url.Action("GetStage", "Standings", new { id = 1 })',
......或者这个:
routes.MapRoute(null, "Standings/{id}",
new { controller = "Standings", action = "Index" }
new { id = UrlParameter.Optional, });