简体   繁体   English

ASP.NET MVC 4编辑器模板和jQuery Datepicker

[英]ASP.NET MVC 4 Editor Template and jQuery Datepicker

I am using a jquery datepicker in my ASP.NET MVC4 Web app. 我在ASP.NET MVC4 Web应用程序中使用了jquery datepicker。 I am also using an editor template for my datetime data type. 我还为我的日期时间数据类型使用了一个编辑器模板。

My editor template looks like this: 我的编辑器模板如下所示:

<input type="text" name="@ViewData.ModelMetadata.PropertyName" class="date" value='@(((DateTime)Model).ToShortDateString())' />

My code for jquery date picker: 我的jQuery日期选择器代码:

$(document).ready(function() {
    $(".date").datepicker();
})

The problem I am running into is that when I load date from the database, the date picker highlights today's date instead of the date in the textbox. 我遇到的问题是,当我从数据库中加载日期时,日期选择器会突出显示今天的日期,而不是文本框中的日期。 How do I get the date picker to highlight the date I loaded into the textbox? 如何获取日期选择器以突出显示加载到文本框中的日期?

$(".date").datepicker({defaultDate: "@(((DateTime)Model).ToShortDateString()"});

Surely though, if you have set @model DateTime at the top of your editor template, you could simply go: 当然,如果在编辑器模板的顶部设置了@model DateTime,则可以简单地进行以下操作:

$(".date").datepicker({defaultDate: "@Model.ToShortDateString()"});

Or in fact, maybe even dynamically like so - NB the id attribute: 或者实际上,甚至可能是动态的-注意id属性:

<input type="text" id="@ViewData.ModelMetadata.PropertyName" name="@ViewData.ModelMetadata.PropertyName" class="date" value='@(((DateTime)Model).ToShortDateString())' />

$(".date").datepicker({defaultDate: $("#@ViewData.ModelMetadata.PropertyName").val()});

Have a play around. 玩一玩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM