2

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

5
  • What is SearchRawInput? Looks like that your List<Int32> is converted to string and StartDate is not formatted like as you want. Commented Aug 27, 2013 at 9:56
  • It seems like your CategoryIDs is not a single value but is a list. Is that what you want.? Commented Aug 27, 2013 at 9:58
  • yes i want it to give me a string like CategoryId=1&CategoryId=2 Commented Aug 27, 2013 at 10:10
  • SearchRawInput is a view model Commented Aug 27, 2013 at 10:11
  • @rajansoft1 You should have included that very helpful comment above of yours in your question at first. I didn't think of what you actually want. Commented Aug 27, 2013 at 10:18

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

What will happen when my list is empty?
If your list is empty (means count=0) then the resulting string is an empty string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.