0

I am aware that the way that dates are dealt with in the querystring use invariant culture. however, i cannot seem to get a solution to deal with this issue to suit my needs.

I have a list of items which I filter using a posted form, the filters are wrapped in a class including various other options along with the start/end dates thus:

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

so the user selected their filters and clicks apply, and as the form is posted (thus adhering to my culture settings dd/MM/yyyy) the filter works fine and away they go. however, when they click through to an item in the returned list, i am persisting the filtering in the querystring, so that they can navigate about and return with their filters still in place. again this is working fine, until of course they filter by a date, when it gets in a kerfuffle and falls over in a heap. In order to generate the links to navigate about I have written a helper which goes something like this:

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());
}

its the urlHelper.Action which when adding in the routeValueDictionary formats the date with the month first. I do have a custom model binder which uses my GB culture - pulled from the culture setting in the web.config - to parse any dates correctly, and also use datepickers which also use the GB format I have done a fair bit of searching on this, and people are suggesting using ticks, or setting dates to the iso yyyy-mm-dd format.

my question is, how would I implement this? I tried changing all date formatting to use the yyyymmdd format but the url.action still decides it wants the mmddyyyy format. I also tried wrapping the datetime filter properties with long ticks properties, but failed with that too..

if anyone can offer some assistance, it would be much appreciated

thanks

nat

nat
  • 2,185
  • 5
  • 32
  • 64

1 Answers1

0

Look At this Thread it will help for you

Try putting your date on the querystring in a format something like this:

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

This will give you a URL like (if you're not using routes):

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

This is helpful because you'll want to avoid slashes in your date on the querystring, and you'll want to be explicit about month/date placement (the whole US and UK). Suggest using numbers for date, and alpha characters for month.

In your ActionMethod, you can then simply TryParse() or Parse() the incoming value.

var dt = DateTime.Parse(date);

In your LINQ query you can do something like this:

&& s.performanceDate == dt 

So to put it all together:

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"}) %>
Community
  • 1
  • 1
Jagadeesh Govindaraj
  • 6,977
  • 6
  • 32
  • 52
  • 1
    hi, thanks for the reply. problem is I am using models to hold the data, so I dont get a chance to deal with the dates separately at all, or i could format them how i wished at that point.. – nat May 29 '15 at 11:08