ASP.Net MVC:通过路由和视图方法请求变量

发布于 2024-07-23 15:18:49 字数 604 浏览 1 评论 0原文

基本上,如果我想创建一个带有分页的搜索页面,我需要一个像这样的网址:

/Topics/Index?search=hi&page=1

我似乎不知道如何:

A)设置没有搜索和页面1的默认路由 /Topics/Index?page=1 甚至 /Topics/Index?search=&page=1

B) 使用 View 方法执行相同的操作

我确实看到,如果我在控件上有一个方法:

Index(String search, Int32? page)

并使用 url:

/Topics/Index?search=hi&page=1 or /Topics/Index?search=hi

它给了我我想要的方法。 我只需要一种方法来获取主题控制器的默认路由,以使用所述请求变量创建默认 url。 我只是不认为

/Topics/Index/hi/1

有利于搜索网址,主要是因为不能保证我会有搜索词或页面,所以它最终可能会像:

/Topics/Index/1

Basically if I wanted to make something a search page with paging I would need a url like:

/Topics/Index?search=hi&page=1

What I can't seem to figure out is how to:

A) Set a default route with no search and page 1
/Topics/Index?page=1 or even /Topics/Index?search=&page=1

B) use the View method to do the same

I do see that if I have a method on the control:

Index(String search, Int32? page)

And use the url:

/Topics/Index?search=hi&page=1 or /Topics/Index?search=hi

It gives me what I want in the method. I just need a way to get a default route for the Topic controller to create a default url with said request variables. I just don't think that

/Topics/Index/hi/1

Is conducive to a search url, mostly because there's no guarantee I'll have search terms or a page so it could end up like:

/Topics/Index/1

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

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

发布评论

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

评论(2

一影成城 2024-07-30 15:18:50

您在 RouteValueDictionary 中传递的任何未映射到 Url 部分的内容都将作为查询字符串参数添加。 所以你可以这样做:

Url.GenerateUrl("Route", "Index", "Topics", 
  new RouteValueDictionary(new 
    { 
      page = this.Model.PageNumber, 
      search = this.Model.Search
    });

Anything you pass in the RouteValueDictionary that doesn't map to a part of your Url will get added as a querystring parameter. So you can do:

Url.GenerateUrl("Route", "Index", "Topics", 
  new RouteValueDictionary(new 
    { 
      page = this.Model.PageNumber, 
      search = this.Model.Search
    });
北城半夏 2024-07-30 15:18:50

所以基本上我通过在控制器上设置默认值来处理非值。 但不确定这是最好的主意。

在 GLobal.asax 中:

routes.MapRoute
(
 "TopicDefault",                                              
 "Topic/{action}",                          
  new { controller = "Topic", action = "Index"}  
);

在控制器上:

public ActionResult Index(Int32? parentForumId, Int32? pageNumber, Int32? amountToShow)
{

  Int32 revisedPageNumber = pageNumber.HasValue ? pageNumber.Value : 0;
  Int32 revisedAmountToShow = amountToShow.HasValue ? amountToShow.Value : 10;
  Int32 revisedParentForumId = parentForumId.HasValue ? parentForumId.Value : 1;

  IList<TopicCreateViewModel> modelValues =
     Topic.GetListForGrid(revisedParentForumId, revisedPageNumber, 
       revisedAmountToShow, out realPage)


  return View("Index", modelValues);
}

So basically I resorted to handling the non values by setting up defaults on the controller. Not sure this is the best idea though.

In GLobal.asax:

routes.MapRoute
(
 "TopicDefault",                                              
 "Topic/{action}",                          
  new { controller = "Topic", action = "Index"}  
);

On the Controller:

public ActionResult Index(Int32? parentForumId, Int32? pageNumber, Int32? amountToShow)
{

  Int32 revisedPageNumber = pageNumber.HasValue ? pageNumber.Value : 0;
  Int32 revisedAmountToShow = amountToShow.HasValue ? amountToShow.Value : 10;
  Int32 revisedParentForumId = parentForumId.HasValue ? parentForumId.Value : 1;

  IList<TopicCreateViewModel> modelValues =
     Topic.GetListForGrid(revisedParentForumId, revisedPageNumber, 
       revisedAmountToShow, out realPage)


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