gpt4 book ai didi

c# - MVC 输入 Action 方法

转载 作者:太空宇宙 更新时间:2023-11-03 12:29:16 24 4
gpt4 key购买 nike

Action 方法的输入有问题。

我有这个代码:

public ViewResult List(int page_number = 1) {

ProductsListViewModel model = new ProductsListViewModel {

Products = repository.Products
.OrderBy(m => m.ProductID).Skip((page_number - 1) * PageSize)
.Take(PageSize),
PagingInfo = new PagingInfo {

CurrentPage = page_number,
ItemsPerPage = PageSize,
TotalItems = repository.Products.Count()


}
};

return View(model);

}

我有这个路由配置:

public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: null,
url: "Page{page}",
defaults: new { Controller = "Product", action = "List" }
);

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

当我键入 URL 时:http://localhost/Page2http://localhost/Page3page_number 的值始终为 1。为什么?

最佳答案

URL模板中的模板参数需要匹配Action中的参数名称。

因此要么更改配置以匹配操作。

routes.MapRoute(
name: null,
url: "Page{page_number}",
defaults: new { Controller = "Product", action = "List" }
);

或更改操作以匹配配置

public ViewResult List(int page = 1) { ... }

关于c# - MVC 输入 Action 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43436526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com