jquery datepicker + Html.TextBoxFor

时间:2010-12-20 12:01:21

标签: jquery datepicker

我对jquerydatepicker和TextBoxFor()方法有疑问。 我有一个asp.net表单,我用它来创建和编辑一个对象。我使用相同的表单进行编辑和创建。 它看起来像这样:

<script>
   $(function () {
     $("#datepicker2").datepicker(
     {
        dateFormat: 'yy.mm.dd'
     });                        
   });
</script>

<div class=demo><%= Html.TextBoxFor(m => m.Distribution.ToDate, new { id = "datepicker2", type = "text" })%></div>

它工作正常,但我的问题是,当我使用编辑时如何格式化日期?对于创建它没关系,因为它将显示一个空文本框,如果我选择一个日期,它将采用正确的格式。在编辑中,它首先显示整个日期时间值,然后我选择一个日期,它以我想要的方式显示该日期。但是我该怎么做,以便它从一开始就按照我想要的方式显示它?

我试着这样做:

<%= Html.TextBoxFor(m => m.Distribution.ToDate.ToShortDateString(), new { id = "datepicker2", type = "text" })%>

但它会出错。

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

<script language="javascript" type="text/javascript">
   $(document).ready(function () {
       $('#startDatePicker').datepicker({ clickInput: true });
   });
</script> 


<% var attributes = (new Dictionary<string, object> { { "Value", Model.Plan.StartDate.ToShortStrin() } }); %>
<% attributes.Add("style", "width:80px; text-align:right"); %>
<% attributes.Add("ID", "startDatePicker"); %>
<%: Html.TextBoxFor(model => model.Plan.StartDate, attributes)%>
相关问题