ASP.NET MVC, DateTimePicker and jQuery validation

I have a model with a start date and an end date:

[Required]
[DataType(DataType.DateTime)]
public DateTime StartTime { get; set; }

[Required]
[DataType(DataType.DateTime)]
public DateTime EndTime { get; set; }

The current view that uses these two dates is as follows:

@if (Roles.IsUserInRole("Teacher"))
{
    <h2>AddSession</h2>

    using (Html.BeginForm("SessionAdded", "Course", FormMethod.Post, new { id = "myform" }))
    {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>Add Session</legend>
            <div class="editor-label">
                @Html.LabelFor(model => model.Name)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.StartTime)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.StartTime, new { @class = "date" , id = "StartTime" })
                @Html.ValidationMessageFor(model => model.StartTime)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.EndTime)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.EndTime, new { @class = "date", id = "EndTime" })
                @Html.ValidationMessageFor(model => model.EndTime)
            </div>
            <p>
                <input id="submit" type="submit" />
            </p>
        </fieldset>
    }
}

When I delete: <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>From my point of view, I have no problem.

But when I use jquery.validatemy two fields are returned: Please enter a valid date.. I am using DateTimePicker for jQuery user interface .

I have the following Javascript code:

$(document).ready(function () {

var datepickers = $('.date').datetimepicker({
    minDate: 0,
    dateFormat: "dd/mm/yy",
    timeFormat: "hh:mm",
    onSelect: function (date) {
        var option = this.id == 'StartTime' ? 'minDate' : 'maxDate';
        datepickers.not('#' + this.id).datepicker('option', option, date);
    }
});

$("#myform").validate({
    ignore: ".date"
})

});

I would ignore the jQuery validation for the two fields Date (class = "date") , because it checks a certain date format, and I have to use it in your application: dd/mm/yyyy hh:mm.

I also tried to make a custom validator, but to no avail. I always get an error Please enter a valid date...

Thank you for your help,

Ed

+3
2

CurrentCulture , , . Hanselman post . , MVC?

, ( GB)

web.config

<globalization culture="en-GB" uiCulture="en-GB" enableClientBasedCulture="false" />   

Global.asax

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

, , ,

public class CultureAwareActionAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            if (!string.IsNullOrEmpty(filterContext.HttpContext.Request["culture"]))
            {
                Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo(filterContext.HttpContext.Request["culture"]);
            }
        }
    }

[CultureAwareAction]

( Telerik, )

+1

. , , , .

Date.cshtml Views\Shared\EditorTemplates, :

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

javascript, - , , script, :

$(document).ready(function () {
    $('.date').datepicker({ dateFormat: "dd/mm/yy", firstDay: 1, maxDate: "+0d" });
    $.validator.methods.date = function () { return true; };
});

, validator.methods.date , true, .

, , IIS , -, pollirrata, IIS .Net Globalization.

+2
source

All Articles