jquery ui datepicker and mvc view model type datetime

前端 未结 3 1221
攒了一身酷
攒了一身酷 2020-11-22 05:00

I am using jquery datepicker on my view model

Here is my view:

@Html.TextBoxFor(o => o.JobStartDate, new { id = \"dt1\", @class = \"input-block-le         


        
相关标签:
3条回答
  • 2020-11-22 05:27

    You can look at using the jquery globalize or add the following to your script (assuming of course that the server culture date format is 'dd/MM/yyy')

    $.validator.addMethod('date', function (value, element) {
      if (this.optional(element)) {
        return true;
      }
      var valid = true;
      try {
        $.datepicker.parseDate('dd/mm/yy', value);
      }
      catch (err) {
        valid = false;
      }
      return valid;
    });
    $('#dt1').datepicker({ dateFormat: 'dd/mm/yy' });
    

    and please use @Html.ValidationMessageFor(m => m.JobStartDate)

    0 讨论(0)
  • 2020-11-22 05:30

    You will need to create a custom validator. You need to modify your property to take a string value instead like this

    public string JobStartDate {get; set; }
    

    You will then need to create your custom validator like this

    public class CheckDateAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext) {
            // Validate your Date here
        }
    }
    

    After this you will just decorate your property as such -

    [CheckDate]
    public string JobStartDate {get; set;}
    

    Here is a good tutorial on custom validators

    0 讨论(0)
  • 2020-11-22 05:38

    Here MVC View Model:

        public DateTime? date { get; set; }
    

    This is for script :

        $('.DatePickers').datetimepicker({
            timepicker: false,
            format: 'd-M-Y',
            scrollInput: false,
            closeOnDateSelect: true
        });
    

    keypress event for readonly date picker

        $(".DatePickers").keypress(function (event) { event.preventDefault(); });
    
    0 讨论(0)
提交回复
热议问题