1

I checked all topic with similar problem but I can't find what I'm doing wrong.

My model for date is:

[Display(Name = "StartDate")]
[DataType(DataType.DateTime), DisplayFormat(DataFormatString = "{0:dd.mm.yy HH:mm}", ApplyFormatInEditMode = true)]
public DateTime? DateFrom { get; set; }

In my cshtml file is:

@Html.LabelFor(m => m.DateFrom)
@Html.TextBoxFor(m => m.DateFrom, new Dictionary<string, object> { { "class", "form-control datetimepicker" } })
@Html.ValidationMessageFor(m => m.DateFrom)

I'm using datetimepicker plugin - http://xdsoft.net/jqplugins/datetimepicker/ which brings me to textbox value in format which I want, but validation throws "The field StartDate must be a date."

Check screenshots

enter image description here enter image description here

DateTimePicker implementation:

$(".datetimepicker").datetimepicker({           
    dateFormat: 'dd.mm.yy',
        timeFormat: 'HH:mm'               
});

Also tried:

$.validator.addMethod('date',
    function (value, element) {
        if (this.optional(element)) {
            return true;
        }
        var ok = true;
        try {
            $.datepicker.parseDateTime('dd.mm.yy HH:mm', value);            
        }
        catch (err) {
            ok = false;
        }
        return ok;
    });

2 Answers 2

0

You've got a Culture problem, chief.

To set the proper culture for your dates, use the following code:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

en-US stands for United States English. Replace that with the correct string for your desired Culture.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried to add it into web config like is mentioned here: stackoverflow.com/questions/7216057/… <globalization uiCulture="sk-SK" culture="sk-SK" /> But it not helps
Can you set the culture on the actual Calendar control?
Also tried your approach Thread.CurrentThread.CurrentCulture = new CultureInfo("sk-SK"); in Global asax in Application_Start
Look at the home page you linked. There's a line of code on it that looks like this: jQuery.datetimepicker.setLocale('de');
0

I think you should divide your controller in two :
-One to get (just add the annotation [HttpGet]),
-One to post (just add the annotation [HttpPost]).

You can then write a personnalised error(with a try catch block for example) in the post method to add the message you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.