Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to change date format to fit dd.MM.yyyy. I am getting client side validation error...

I've tried everything:

Web.config:

<globalization uiCulture="ru-RU" culture="ru-RU" />

Model:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)]
public DateTime? ServiceCreatedFrom { get; set; }

Editor template:

@model DateTime?
@Html.TextBox(string.Empty, (Model.HasValue ? Model.Value.ToString("dd.MM.yyyy") : string.Empty), new { @class = "date" })

View:

@Html.EditorFor(m => m.ServiceCreatedFrom, new { @class = "date" })

Even Global.asax:

public MvcApplication() //Constructor
{
    BeginRequest += (sender, args) =>
        {
            var culture = new System.Globalization.CultureInfo("ru");
            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;
        };
}

Please help! I spent a lot of time on such a "trivial" stuff!!!

share|improve this question
1  
i had a similar problem: stackoverflow.com/questions/13623381/… – Snoopy Jun 14 at 10:36
ugly, but probably the only way :( – Andrei Mikhalevich Jun 14 at 10:39
Snoopy! Thanks for your short comment! It helped A LOT! – Andrei Mikhalevich Jun 14 at 11:25
1  
yes, an ugly way. this should be done by the framework ;-) – Snoopy Jun 14 at 12:44
Absolutely . . . . – Andrei Mikhalevich Jun 14 at 14:01

2 Answers

up vote 1 down vote accepted

The following should work:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)]
public DateTime? ServiceCreatedFrom { get; set; }

and in your editor template:

@model DateTime?
@Html.TextBox(
    string.Empty, 
    ViewData.TemplateInfo.FormattedModelValue, 
    new { @class = "date" }
)

and then:

@Html.EditorFor(x => x.ServiceCreatedFrom)

The second argument you were passing to the EditorFor call doesn't do what you think it does.

For this custom editor template, since you specified the format explicitly on your view model property the <globalization> element in your web.config and the current thread culture will have 0 effect. The current thread culture is used with the standard templates and when you didn't override the format with the [DisplayFormat] attribute.

share|improve this answer
Hehe I knew somebody would be quicker :). – sammy34 Jun 14 at 10:19
Thanks for the answer. But I am still getting validation exception, just because app expects MM.dd.yyyy – Andrei Mikhalevich Jun 14 at 10:22
Would it be overkill to add a custom model binder for this case? I'm not sure if the default model binder considers the current culture. – sammy34 Jun 14 at 10:24
2  
Yes, the default model binder uses the current culture. But there's one more ting to the picture that you should not forget. If you enabled client side validation (unobtrusive javascript) then the date is parsed on the client, using the client browser culture. So if you are using client side validation you will also have to write a custom rule to parse the date on the client using the specified format instead of using the browser settings. – Darin Dimitrov Jun 14 at 10:26
1  
You will have to write a custom date parsing method for the jquery.validate plugin that you are using: stackoverflow.com/questions/11756226/… – Darin Dimitrov Jun 14 at 10:34
show 8 more comments

As a potential aid for identifying the issue, are you able to: 1. Set a breakpoint at the point where you're trying to format the date 2. Use something like the Immediate Window in Visual Studio to evaluate the value of

Thread.CurrentThread.CurrentCulture.Name

If you do this, does it come back with the "ru-RU" culture?

I'm sure I'm not the only one that would be happy to help you work through debugging this. That said, perhaps somebody quicker than me can see the problem straight away :).

Edit: It looks like you're using Razor, so you should be able to set a breakpoint directly in the view file on the line where you're trying to format the date.

Edit #2:

There may be a cleaner way to do this, but if the form data is being posted in dd.MM.yyyy then you might need a custom model binder, something like:

public class CustomModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
              // custom bind the posted date    
        }
}

...which would then get assigned as a model binder in e.g. ApplicationStart in Global.asax.cs.

Let me know if you think this might help and I can elaborate.

share|improve this answer
The culture is ru-RU. I've checked :) – Andrei Mikhalevich Jun 14 at 10:19
Thank you all!!! – Andrei Mikhalevich Jun 14 at 11:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.