2012-07-10 46 views
3

我有2個重要的問題。如何在mvc 3 EditorFor中使用datepicker?

1)DateTimePicker在<input type="text" name="date" id="date"/>上運行良好,但沒有css效果和顏色。 2)<%Html.EditorFor(m => m.StartDateTime, new { @class = "datepicker" })%>不起作用。沒有顯示datetimepicker。

如何使jquery datetimepicker。我引用的文章是:http://geekswithblogs.net/michelotti/archive/2010/02/05/mvc-2-editor-template-with-datetime.aspx


<link rel="stylesheet" href="../../Styles/jquery.ui.all.css" type="text/css" /> 
<script type="text/javascript" src="<%= Url.Content("~/Scripts/jquery-1.7.2.min.js") %>"></script> 
<script type="text/javascript" src="<%= Url.Content("~/Scripts/jquery-ui.js") %>"></script> 
<script type="text/javascript" src="<%= Url.Content("~/Scripts/jquery.ui.datepicker.js") %>"></script> 

<script type="text/javascript"> 
    $(document).ready(function() { 
     $('.datepicker').datepicker({}); 
     $('#date').datepicker({}); 

    }); 
</script> 





<table> 
<tr><td><input type="text" name="date" id="date" /></td></tr> 
<tr><td><%:Html.LabelFor(m=>m.StartDateTime) %></td><td><%:Html.EditorFor(m => m.StartDateTime, new { @class = "datepicker" })%></td></tr> 
</table 

回答

3

程序員,這是我是如何做了一會兒回來。

創建一個名爲DateTime.aspx的editorfor模板:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime?>" %> 

<%string name = ViewData.TemplateInfo.HtmlFieldPrefix;%> 
<%string id = name.Replace(".", "_");%> 

<div class="editor-label"> 
    <%= Html.LabelFor(model => model) %> 
</div> 
<div class="editor-field"> 
    <%= Html.TextBox("", (Model.HasValue ? Model.Value.ToString("dd-MM-yyyy") : string.Empty), new { @class = "date" }) %> 
    <%= Html.ValidationMessageFor(model => model)%> 
</div>  

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#<%=id%>").datepicker({ 
      dateFormat: 'dd-mm-yy', 
      changeMonth: true, 
      changeYear: true, 
      showOn: 'button', 
      buttonImage: '<%=Url.Content("~/Content/images/calendar.gif") %>' 
     }); 
    }); 
</script> 

然後簡單地調用它是這樣在你看來:

<%= Html.EditorFor(m => m.StartDate) %> 

歡呼聲......

1

嘗試使用Html.EditorFor的Html.TextBoxFor代替:

<%:Html.TextBoxFor(m => m.StartDateTime, new { @class = "datepicker" })%> 

對於CSS問題,使用:

<link rel="stylesheet" href="<%= Url.Content("~/Styles/jquery.ui.all.css") %>" type="text/css" /> 
+0

遺憾沒工作: ( – programmerist 2012-07-10 12:47:54

3

你的第二個問題,請使用Html.TextBoxFor而不是EditorFor來獲取html屬性。

Html.TextBoxFor(model => model.Date, new { @class = "datePick", Value = Model.Date.ToString("MM/dd/yyyy") }) 

對於你的第一個問題,你的路徑是否正確?我使用Url.Content來獲得它