rajansoft1
rajansoft1

Reputation: 1356

Using List<int> in Url.Action Method

i am writing code for search page and i have to pass some filters to the action and depending on those input I have to generate hyper links, hence i am using Url.Action function to generate links.

below is my code

@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
})

and it is generating url like this

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

My expected Url was

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

Upvotes: 2

Views: 1311

Answers (1)

dasheddot
dasheddot

Reputation: 2966

What about converting it to string representation yourself like that:

@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
})

That is what a viewmodel should be for. That you convert and format all the values you need in the way the view expects it. Notice that I added a ToShortDateString() to your dates as well, since it seems you are not interested in the time part.

Upvotes: 2

Related Questions