2013-08-27 21 views
2

我正在編寫搜索頁的代碼,我必須通過一些過濾器的行動,並根據那些輸入,我必須生成超鏈接,因此我使用Url.Action函數來生成鏈接。在Url.Action中使用列表<int>方法

下面

是我的代碼

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

並且它產生像URL這

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 &您做生意= 2

我的預期地址是

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

+0

什麼是'SearchRawInput'?看起來你的'List '被轉換爲'string',而'StartDate'沒有按照你的意願格式化。 –

+0

看起來你的CategoryIDs不是一個單一的值,而是一個列表。那是你要的嗎。? – serene

+0

是的,我想它給我一個字符串像CategoryId = 1和CategoryId = 2 – rajansoft1

回答

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(),因爲您似乎對時間部分不感興趣。

+0

謝謝,它爲我工作 – rajansoft1

+0

當我的清單是空的時會發生什麼? – rajansoft1

+0

如果您的列表爲空(意味着count = 0),則結果字符串爲空字符串。 – dasheddot

相關問題