控制器方法中参数的默认值覆盖所有内容

发布于 2024-09-16 06:55:28 字数 571 浏览 1 评论 0原文

你好,我刚刚开始学习 mvc2,我对参数页面的默认值有疑问(你可以看到下面的方法)。

无论我在 URL 中输入什么内容,它始终为 0。例如,这个

h.ttp://localhost:52634/Products/List/2

应该显示第 2 页,但在调试模式下,页面参数为 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);
    }

Hello i have just started learning mvc2 and im having a problem with the default value for the parameter page(you can see the method below).

Its always 0 regardless of what i type in the URL. For example this

h.ttp://localhost:52634/Products/List/2

should show page 2 but when in debug mode the page parameter is 0, so im always getting the first page of the list in my view.

i am using the predefined standard routes in global asax when you start a new mvc2 project.

am i missing something?

//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);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

煮茶煮酒煮时光 2024-09-23 06:55:28

这是一个路由问题,默认路由指定一个 id 属性,您正在使用一个名为 page 的属性。我自己是 MVC 新手,但在默认路由之前添加此路由:

routes.MapRoute("MyRoute", "{controller}/{action}/{page}",
    new { controller = "Foo", action = "List", page = UrlParameter.Optional });

It's a routing issue, the default route specifies an id property, you're using a property called page. I'm new to MVC myself, but add this route before the default route:

routes.MapRoute("MyRoute", "{controller}/{action}/{page}",
    new { controller = "Foo", action = "List", page = UrlParameter.Optional });
随梦而飞# 2024-09-23 06:55:28

删除“= 0”,并执行以下操作:

public ViewResult List(int? page)
{
    int val = page.GetValueOrDefault(0);

并在各处使用 val 而不是 page。那应该有效。如果不行那就是路由问题了。

HTH。

Remove the " = 0", and do:

public ViewResult List(int? page)
{
    int val = page.GetValueOrDefault(0);

And use val everywhere instead of page. That should work. If not, it's an issue with routing.

HTH.

岁月静好 2024-09-23 06:55:28

我知道现在回答已经很晚了。由于 MVC 的默认路由如下,

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

因此参数名称应该是 id。现在您有两个选项,要么将参数名称更改为 id,要么另一个选项是在 App_Start 文件夹下的route.config 文件中定义您自己的路由。

I know it's very late to answer. As default route for MVC is following

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

which is expecting that parameter name should be id. Now you have 2 options here either change your parameter name to id or the other option is define your own route in route.config file which is under App_Start folder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文