您好我刚开始学习mvc2并且我对参数页面的默认值有疑问(您可以看到下面的方法)。
无论我在URL中键入什么,它始终为0。例如这个
h.ttp://本地主机:52634 /产品/列表/ 2
应显示第2页,但在调试模式下,page参数为0,因此我总是在视图中获取列表的第一页。
当你启动一个新的mvc2项目时,我在全局asax中使用预定义的标准路由。
我错过了什么吗?//This is the ProductsController
public ViewResult List(int page = 0)
{
var products = productsRepo.Products()
//send in source, current page and page size
productList = new PagedList<Product>(products, page, 10);
return View(productList);
}
答案 0 :(得分:3)
删除“= 0”,然后执行:
public ViewResult List(int? page)
{
int val = page.GetValueOrDefault(0);
并且使用val到处而不是页面。这应该工作。如果没有,那就是路由问题。
HTH。
答案 1 :(得分:3)
这是路由问题,默认路由指定id
属性,您使用的是名为page
的属性。我自己是MVC的新手,但在默认路由之前添加此路由:
routes.MapRoute("MyRoute", "{controller}/{action}/{page}",
new { controller = "Foo", action = "List", page = UrlParameter.Optional });
答案 2 :(得分:0)
我知道答案已经很晚了。由于遵循MVC的默认路由
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
,它期望参数名称应为id。现在,这里有2个选项,您可以将参数名称更改为id,或者另一个选项是在App_Start文件夹下的route.config文件中定义自己的路由。