【问题标题】:jquery datepicker + Html.TextBoxForjquery datepicker + Html.TextBoxFor
【发布时间】:2010-12-20 12:01:21
【问题描述】:

我对 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" })%>

但它给出了一个错误。

有什么建议吗?

【问题讨论】:

    标签: jquery datepicker


    【解决方案1】:
    <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)%>
    

    【讨论】:

      猜你喜欢
      • 2013-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-08
      • 2011-08-23
      相关资源
      最近更新 更多