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;
    });
tonco
  • 1,281
  • 3
  • 16
  • 30

2 Answers2

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.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • I tried to add it into web config like is mentioned here: http://stackoverflow.com/questions/7216057/setting-culture-for-asp-net-mvc-application-on-vs-dev-server-and-iis But it not helps – tonco Oct 02 '15 at 08:14
  • Can you set the culture on the actual Calendar control? – Robert Harvey Oct 02 '15 at 08:17
  • Also tried your approach Thread.CurrentThread.CurrentCulture = new CultureInfo("sk-SK"); in Global asax in Application_Start – tonco Oct 02 '15 at 08:18
  • 1
    Look at the home page you linked. There's a line of code on it that looks like this: `jQuery.datetimepicker.setLocale('de');` – Robert Harvey Oct 02 '15 at 08:18
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.

Kyle_jumpen
  • 111
  • 2
  • 9