2015-05-29 29 views
0

我知道查詢字符串中使用不變文化處理日期的方式。然而,我似乎無法得到一個解決方案來處理這個問題,以滿足我的需求。Querystring中使用url.action出錯的日期

我有I濾波器使用貼形式中,過濾器被包裹在包括與開始/結束沿各種其它選項的類的項目的列表日期從而:

public class FilterOptions{ 
    public int? BrandID{get;set;} 
    public string SearchTerm{get;set;} 
    public DateTime? FromDate{get;set;} 
    public DateTime? ToDate{get;set;} 
    //etc.... 
} 

所以用戶選擇他們的過濾器和點擊適用,並且隨着表格發佈(因此堅持我的文化設置dd/MM/yyyy),過濾器可以正常工作,並且可以繼續使用。然而,當他們點擊返回列表中的項目時,我堅持在查詢字符串中進行篩選,以便他們可以瀏覽並返回其篩選器。 再次這工作正常,直到他們過濾一個日期,當它進入一個kerfuffle並在堆中摔倒。 爲了生成鏈接導航約我寫了一個幫手,其是這樣的:

public static HtmlString NavigationActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, FilterOptions filter) 
{ 
    if (string.IsNullOrEmpty(controllerName)) 
    { 
     controllerName = (string)htmlHelper.ViewContext.RouteData.GetRequiredString("controller"); 
    } 
    RouteValueDictionary filterRVD = new RouteValueDictionary(filter); 
    RouteValueDictionary rvd = new RouteValueDictionary(routeValues); 

    RouteValueDictionary routeCombined = new RouteValueDictionary(filterRVD.Union(rvd).ToDictionary(k => k.Key, k => k.Value)); 

    var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext); 
    var anchor = new TagBuilder("a"); 
    anchor.InnerHtml = linkText; 
    anchor.Attributes["href"] = urlHelper.Action(actionName, controllerName, routeCombined); 
    return MvcHtmlString.Create(anchor.ToString()); 
} 

了其在routeValueDictionary加時urlHelper.Action格式的月份第一日。 我有一個自定義模型綁定器,它使用我的GB文化 - 從web.config中的文化設置中拉出 - 正確解析任何日期,並使用也使用GB格式的datepickers 我已經完成了一些搜索對此,人們建議使用刻度或將日期設置爲iso yyyy-mm-dd格式。

我的問題是,我將如何實現這一點?我嘗試更改所有日期格式以使用yyyymmdd格式,但url.action仍然決定使用mmddyyyy格式。 我也試過包裝用長蜱屬性的日期時間篩選器屬性,但與太失敗了..

如果有人能提供一些幫助,這將是非常讚賞

感謝

NAT

回答

0

看看這個線程將有助於you

嘗試把你的日期放在查詢字符串中,格式如下:

item.performanceDate.ToString("dd-MMM-yyyy") 

這會給你像一個URL(如果你不使用的路線):

http://foo/Performance/Details?name=someName&venue=someVenue&date=31-Mar-2011 

這是有用的,因爲你要避免在查詢字符串的日期斜線,你將要明確月/日的位置(整個美國和英國)。建議使用日期的數字和月份的字母字符。

在你的ActionMethod中,你可以簡單地將TryParse() or Parse()的傳入值。

var dt = DateTime.Parse(date); 

在你的LINQ查詢,你可以做這樣的事情:

&& s.performanceDate == dt 

所以把它放在一起:

public ActionResult Details(String name, String venue, String date) 
{ 
    DateTime dt; 

    if (DateTime.TryParse(date, out dt)) 
    { 
     return View(_repository.performanceDetails(name, venue, dt));  
    } 
    else 
    { 
     return View("Error", "The value passed in date isn't a date value."); 
    } 
} 

public PerformanceDetails performanceDetails(String name, String venue, DateTime dt) 
{ 
    var PerformanceDetails = (from s in _db.PerformanceDetails 
           where s.show == name && 
            s.venue == venue && 
            s.performanceDate == dt.Date     
           select s).First(); 
    return PerformanceDetails; 

} 

<%= Html.ActionLink("View Details", "Details", "Performances", 
       new { name = item.show , 
         venue = item.venue, 
         date = item.performanceDate.ToString("dd-MMM-yyyy") }, 
       new {@class = "button"}) %> 
+0

您好,感謝您的答覆。問題是我使用模型來保存數據,所以我沒有機會單獨處理日期,或者我可以格式化他們,我希望在那一點上.. – nat