Passing a DateTime to controller via URL causing error in ASP .NET MVC 3 (culture)

前端 未结 4 1672
轻奢々
轻奢々 2021-02-04 02:45

My application is setted with pt-BR culture (Date is dd-mm-yyyy) in web.config:



        
4条回答
  •  春和景丽
    2021-02-04 02:56

    There's a gotcha with the default model binder that is not easy to know about but once you know it you no longer make the same mistake:

    • When you use a POST request, the default model binder uses your culture settings to parse the dates.

    • When you use a GET request, the default model binder uses CultureInfo.InvariantCulture to parse the dates and ignores your current culture settings.

    Since you are using a GET request and passing the date as a query string parameter, you should format it using the invariant culture format when sending it in the url. The correct way to format your date as a query string parameter is yyyy-MM-dd.

    You may take a look at the following blog post which gets into more details.

提交回复
热议问题