Tuesday, June 26, 2012


ASP.net MVC3 Date Formats validation Issued (dd/mm/yyyy vs mm/dd/yyyy) 


Client side  passing date format as "dd/mm/yyyy" format. but model is return format is incorrect its cheeking mm/dd/yyyy. i have found that issue in mvc web application. 
solution

Reason is that web server culture info is used as "en-us" its validate format "mm/dd/yyyy".  then i found that we can change the culture dynamically in the following way .

Option 1: Page level

The Culture used by the individual Page can be set via the Culture property.
<%@ Page Culture="en-SG" Language="C#" %>

Option 2: Application wide (web.Config)

The Culture can be set Application wide by adding the  node to the web.Config file. The  node must be placed inside .
<system.web>
  <globalization culture="en-SG"/>

Option 3: Thread level

The Culture of the current Thread can be changed programatically be setting the System.Threading.Thread.CurrentThread.CurrentCulture property.
The following example demonstrates how to get the user-agent(browsers) preferred UserLanguage property on each Request and set the Threads CurrentCulture during the Application_BeginRequest Event.
protected void Application_BeginRequest(Object sender, EventArgs e)
{
    CultureInfo requestCulture;
    try
    {        
        requestCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages[0]);
    }
    catch
    {
        // Return server Culture if none available in HttpHeaders.
        requestCulture = CultureInfo.CurrentCulture;
    }
    System.Threading.Thread.CurrentThread.CurrentCulture = requestCulture;
}

No comments:

Post a Comment