【发布时间】:2013-08-20 14:25:34
【问题描述】:
我有这个
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
当我尝试使用像
这样的参数时[AcceptVerbs("POST")]
public JsonResult ShowProductsPerPage(string pageNumber)
{ ...
它不工作
但是当我使用
[AcceptVerbs("POST")]
public JsonResult ShowProductsPerPage(string id)
{ ...
一切正常。
这里是JS
var currentUrl = window.location.protocol + '//' + window.location.host;
var url = currentUrl + "/Products/ShowProductsPerPage/" + pageNumber;
$.ajax({
type: "POST",
url: url,
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data.pageNumber);
}
});
我在 JavaScript 中使用变量 pageNumber,所以我想保留相同的名称
方法背后的代码。
有可能吗?
【问题讨论】:
-
只需将路由中的 {id} 更改为 {pageNumber}。
-
向我们展示您如何将数据发布到控制器
-
@Dennisch 但是当我使用
id时,其他方法呢?他们不会工作。 -
嗯,你不应该有两条参数完全相同的路由,只是名称不同。您可以在查询字符串中添加 pageNumber,并将其作为可选参数添加到路由中。
-
@Dennisch 是的,我知道。我想更改主要参数,它不可能像
idalways...
标签: c# javascript ajax asp.net-mvc asp.net-mvc-routing