【问题标题】:Using List<int> in Url.Action Method在 Url.Action 方法中使用 List<int>
【发布时间】:2023-04-09 00:59:02
【问题描述】:

我正在为搜索页面编写代码,我必须将一些过滤器传递给操作,并且根据这些输入我必须生成超链接,因此我使用 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

【问题讨论】:

  • 什么是SearchRawInput?看起来您的 List&lt;Int32&gt; 已转换为 string 并且 StartDate 的格式不符合您的要求。
  • 您的 CategoryID 似乎不是单个值,而是一个列表。这就是你想要的吗?
  • 是的,我希望它给我一个像 CategoryId=1&CategoryId=2 这样的字符串
  • SearchRawInput 是一个视图模型
  • @rajansoft1 您首先应该在您的问题中包含您上面那条非常有用的评论。我没有想到你真正想要什么。

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


【解决方案1】:

像这样自己将其转换为字符串表示怎么样:

@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(),因为您似乎对时间部分不感兴趣。

【讨论】:

  • 当我的列表为空时会发生什么?
  • 如果您的列表为空(表示 count=0),则结果字符串为空字符串。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-17
  • 1970-01-01
  • 2012-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多