开发者

ASP.NET MVC: jQuery UI DatePicker Set minDate and maxDate from Model on AJAX Loaded Page

I have a dialog form that pops up and its content is loaded using the jQuery load function. On that ajax loaded page there is a jQuery UI datepicker and in the Model that I pass to the view, I have MinDate and MaxDate properties that are used to set the min and max dates of the datepicker. On a non-ajax page I usually do something like this:

<script type="text/javascript">
$(function () {
     var model = @(Model.ToJson());

        $('.date').datepicker('option', 'minDate', new Date(parseInt(model.MinDate.substr(6))));
        $('.date').datepicker('option', 'maxDate', new Date(parseInt(model.MaxDate.substr(6))));

});
</script>

The problem is that if I include any javascript in the view that is loaded from the jQuery load function, it is stripped out and not executed. So, if I included the above code to specify the min and max dates for the datepicker in the dialog form doesn't work.

How can I specify min and max dates for a jQuery UI datepicker where the datepicker is being loaded via ajax and the min 开发者_StackOverflowand max dates come from the model?


You could serialize those two model properties as HTML5 data-* attributes somewhere in the DOM. For example as part of the datepicker itself it would be great:

<input type="text" class="date" name="myDate" data-min="@Model.MinDate" data-max="@Model.MaxDate" />

and then define some functions:

function initPicker() {
    $('.date').each(function() {
        $(this).datepicker('option', 'minDate', $(this).data('min'))
               .datepicker('option', 'maxDate', $(this).data('max'));
    });
}

and then invoke this function in your DOM ready:

$(initPicker);

as well as in your AJAX success callbacks in order to reattach them.


var minshowdate = new Date(@(DateTime.Now.Date.Year), 
                           @(DateTime.Now.Date.Month), 
                           @(DateTime.Now.Date.Day));
$('#ShowDate').datepicker({ minDate : minshowdate });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜