在Url.Action方法</int>中使用List <int>

时间:2013-08-27 09:53:01

标签: c# asp.net-mvc razor asp.net-mvc-routing

我正在为搜索页面编写代码,我必须将一些过滤器传递给操作,并根据这些输入我必须生成超链接,因此我使用Url.Action函数生成链接。

下面是我的代码

@Url.Action("Index","Search",new SkillKindleWeb.ViewModels.Search.SearchRawInput()
{
  CategoryIds = Model.Request.CategoryIds,
  SubCategoryIds = Model.Request.SubCategoryIds,
  StartDate = Model.Request.StartDate,
  EndDate = Model.Request.EndDate,
  StartPrice = Model.Request.StartPrice,
  LocationGroupIds = Model.Request.LocationGroupIds,
  LocationIds = Model.Request.LocationIds,
  EndPrice = Model.Request.EndPrice,
  City = Model.Request.City,
  PageNo = 1,
  SearchQuery = Model.Request.SearchQuery,
  Segment1 = Model.Request.Segment1,
  Segment2 = Model.Request.Segment2,
  TargetAge = Model.Request.TargetAge
})

它正在生成这样的网址

http://someDomain.com/ncr/classes?CategoryIds= System.Collections.Generic.List%601%5BSystem.Int32%5D &安培;起始日期= 03%2F30%2F2013%2000%3A00 3A00%&安培; StartPrice = 0&安培; EndPrice = 140000&安培;您做生意= 2

我的预期网址是

http://SomeDomain.com/ncr/classes?CategoryIds=9&StartDate=3/30/2013&StartPrice=0&EndPrice=140000

1 个答案:

答案 0 :(得分:2)

如何将它自己转换为字符串表示:

@Url.Action("Index","Search",new SkillKindleWeb.ViewModels.Search.SearchRawInput()
{
  CategoryIds = string.Join(",", Model.Request.CategoryIds),
  SubCategoryIds = string.Join(",", Model.Request.SubCategoryIds),
  StartDate = Model.Request.StartDate.ToShortDateString(),
  EndDate = Model.Request.EndDate.ToShortDateString(),
  StartPrice = Model.Request.StartPrice,
  LocationGroupIds = Model.Request.LocationGroupIds,
  LocationIds = Model.Request.LocationIds,
  EndPrice = Model.Request.EndPrice,
  City = Model.Request.City,
  PageNo = 1,
  SearchQuery = Model.Request.SearchQuery,
  Segment1 = Model.Request.Segment1,
  Segment2 = Model.Request.Segment2,
  TargetAge = Model.Request.TargetAge
})

这就是视图模型的用途。您可以按视图所期望的方式转换和格式化所需的所有值。 请注意,我在您的日期中添加了ToShortDateString(),因为您似乎对时间部分不感兴趣。