2

我正在为搜索页面编写代码,我必须将一些过滤器传递给操作,并且根据这些输入我必须生成超链接,因此我使用 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 &StartDate=03%2F30%2F2013%2000%3A00%3A00&StartPrice=0&EndPrice=140000&PageNo=2

我预期的网址是

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

4

1 回答 1

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()也在您的日期中添加了 a,因为您似乎对时间部分不感兴趣。

于 2013-08-27T10:10:33.570 回答