Asp.net mvc 如果您使用的参数是MVC中的类,那么如何在RouteConfig中格式化QueryString?
我在控制器中有这段代码,它接受一个类作为参数Asp.net mvc 如果您使用的参数是MVC中的类,那么如何在RouteConfig中格式化QueryString?,asp.net-mvc,class,pagination,query-string,routeconfig,Asp.net Mvc,Class,Pagination,Query String,Routeconfig,我在控制器中有这段代码,它接受一个类作为参数 public ActionResult Index(PaginationModel paginationViewModel = null) { //do some logic here return View(viewModel); } 这是模型的样子 public class PaginationModel { protected const int DisplayPageRange = 2; //Paginat
public ActionResult Index(PaginationModel paginationViewModel = null)
{
//do some logic here
return View(viewModel);
}
这是模型的样子
public class PaginationModel
{
protected const int DisplayPageRange = 2;
//Pagination Related Properties
public int CurrentPage { get; set; }
public int TotalPages { get; set; }
public int TotalItems { get; set; }
public string PageName { get; set; }
public int ItemsPerPage { get; set; }
public int DefaultItemsPerPage { get; set; }
public string PagingText { get; set; }
//Filters
public string Search { get; set; }
public string Category { get; set; }
public string Star { get; set; }
public string Director { get; set; }
public string Writer { get; set; }
}
我在视图中调用该操作,如
<a href="/?Category=@genre">@genre</a>
目前正在放映
Movie/Index?Category=Horror&Page=1
我可以使用单独的参数而不是像
public ActionResult Index(string category, int pagenum, etc etc)
{
}
但是,有没有一种方法可以在RouteConfig中使用类作为参数来实现这一点
编辑:
我的路线图是
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "MovieSearch",
url: "Movie/Index/{Category}",
defaults: new { controller = "Movie", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "AdminSearch",
url: "{controller}/{action}/{key}",
defaults: new {controller = "Admin", action = "Search", id = UrlParameter.Optional}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Movie", action = "Index", id = UrlParameter.Optional }
);
}
你需要创建一个特定的路由--
url:“Movie/Index/{Category}/{CurrentPage}
并在默认路由之前找到它。我这样做了,它在默认路由之前,但我猜仍然是默认路由?你还没有显示你所做的事情!(我们怎么能猜到你的错误)它没有{CurrentPage}
参数(虽然不确定您的意思是CurrentPage
还是Page
),但您需要使用@Html.ActionLink()
来生成它(或者Url.Action()
属性的Url.Action()
,如果您想手动生成
标记,那么更改为使用ActionLink实际上解决了这个问题。谢谢大家!投票
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "MovieSearch",
url: "Movie/Index/{Category}",
defaults: new { controller = "Movie", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "AdminSearch",
url: "{controller}/{action}/{key}",
defaults: new {controller = "Admin", action = "Search", id = UrlParameter.Optional}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Movie", action = "Index", id = UrlParameter.Optional }
);
}