Creating an ActionLink with a DateTime in a query string in ASP.NET MVC

I have a search form with DateTime search criteria, as well as some other criteria:

<form method="get" action="/app/search">
    <input type="text" value="13/01/2010" name="BeginDate"/>
    <input type="text" value="blah" name="SomeOtherCriterion"/>
<form>

So, I have a search controller with a default action (let it be called Index) and with the SearchCriteria parameter.

public class SearchController
{
    public ActionResult Index(SearchCriteria searchCriteria) {//blah }
}    

public class SearchCriteria
{
    public DateTime BeginDate {get; set;}
    public string SomeOtherCriterion {get; set;}
}

Now, if I want to create an ActionLink by passing the value of SearchCriteria, this way:

Html.ActionLink("Search", "Index", searchCriteria)

I get a BeginDate query string parameter in US format. Looking at Google and digging into System.Web.Routing using Reflector seems to be because it uses InvariantCulture, so there is nothing I can do about it.

It seems that no one has asked this question before, so I think I'm doing something very stupid .... Please help!

EDIT: SearchCriteria ActionLink, , , ToString().

+5
7

, , , InvariantCulture, , , .

- MVC - Route ParsedRoute, RouteBase, , .

, DateTime SearchCriteria, ( ), .

SearchCriteria DateTime :

public class MyDateTime
{
  public DateTime Value { get; set; }
  //for passing MyDateTime in place of a DateTime without casting
  public static implicit operator DateTime(MyDateTime instance) { return instance.Value; }
  //so you can assign a MyDateTime from a DateTime without a cast
  //- e.g. MyDateTime dt = DateTime.Now
  public static implicit operator MyDateTime(DateTime instance) { return new MyDateTime() { Value = instance }; }
  //override ToString so that CultureInfo.CurrentCulture is used correctly.
  public override string ToString()
  {
    return Value.ToString(CultureInfo.CurrentUICulture);
  }
}

.

, , (,.Days ..) DateTime SearchCriteria: MyDateTime, DateTime Value .Value.Member.

+3

, "", , DateTime .

:

Year [ ] Month [ ] Day [ ]

, , (//) (//).

+2

ActionLink? :

Html.ActionLink("Search", 
                "Index", 
                new {BeginDate = 
                      DateTime.Now.ToString("d", new CultureInfo("pt-BR");})

, BeginDate DateTime... , , ?

+1

ISO ( "s" - YYYY-MM-DDTHH: MM: SS). , JavaScript .

+1

, Model Binder ? , ... , . , :)

+1
poking around in System.Web.Routing using Reflector it

, InvariantCulture

? Modelbinding UrlBuilding, , CurrentCulture. , , CurrentCulture ?

+1

Get the ASP.NET MVC 1.0 book written by Scott Hanselman, Scott Guthrie, Phil Haack, and Rob Coniri. They really make this exact script in the book. They use a specific route. I look at him right now on page 216.

They do this by breaking day, month and year. Then you are responsible for using these values ​​as they return.

+1
source

All Articles