2011-08-21 27 views
1

我想爲我的任何模型中的每個datatime對象配置EditorFor模板。對於DateTime對象的這個簡單的EditorTemplate問題

我得到這個錯誤:

\EditorTemplates\DateTime.cshtml(1): error CS1973: 'System.Web.Mvc.HtmlHelper' has no applicable method named 'TextBox' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

這裏就是整個代碼,就是所謂的EditorTemplate,在EditorTemplates文件夾DateTime.cshtml場所內。

任何想法?

這就是我所說的,我認爲:

<div class="editor-label"> 
    @Html.LabelFor(model => model.DateOfBirth) 
</div> 
<div class="editor-field"> 
    @Html.EditorFor(model => model.DateOfBirth) 
    @Html.ValidationMessageFor(model => model.DateOfBirth) 
</div> 

這是自定義EditorTemplate我做:

@Html.TextBox("", Model, new { @class = "date-selector" }); 

THE EDITOR IS WORKING 

基本上,在我的模型每DateTime對象,我想補充的類「日期選擇器」到該HTML元素。這是用於jQueryUI的目的。

我在做什麼錯?

+0

看來我只是把模型轉換爲(日期時間)模型。 –

回答

3

您可以創建如下的擴展方法:

@Html.CalenderTextBoxFor(model => model.Employee.HireDate) 
4

您需要定義editortemplate的模型類型。添加 「@model日期時間」 的CSHTML文件的頂部:

@model DateTime 
@Html.TextBox("", Model, new { @class = "date-selector" }); 
THE EDITOR IS WORKING 
+0

比我的鑄造動態對象的解決方案要乾淨得多。謝謝! :) –

0

public static MvcHtmlString CalenderTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) 
    { 
     return htmlHelper.TextBoxFor(expression, "{0:M/dd/yyyy}", new { @class = "datepicker text" }); 
    } 

然後在視圖下方使用

我知道這是一個老問題,但我有同樣令人沮喪的問題,我不想創建一個適用於所有DateTime值的EditorTemplate(有時在我的UI中顯示時間而不是JQueryUI下降下拉日曆)。在我的研究中,根問題,我碰到有:

  • 標準TextBoxFor幫手讓我應用自定義類「日期選取器」來呈現不顯眼jQueryUI的壓延機,但TextBoxFor不會格式沒有時間的日期時間,因此導致日曆渲染失敗。
  • 標準EditorFor將顯示日期時間爲一個格式化字符串(當與適當的屬性,如[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]裝飾,但它不會讓我將自定義「日期選取器」類。

因此,我擴大在@阿什拉夫答案,並創建自定義HtmlHelper類具有以下作用:

  • 的方法自動將日期時間轉換成由JQuery的日曆所需的ShortDateString(如果時間存在jQuery將失敗) 。
  • 默認情況下,助手將應用所需的htmlAttributes來顯示JQuery日曆,但如果需要可以覆蓋它們。
  • 如果日期爲空,則MVC會將1/1/0001的日期作爲值。該方法用空字符串替換。

我在此發佈此信息,希望能在某些時候幫助其他人。任何需要更改的建議都歡迎。

public static MvcHtmlString CalenderTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes = null) 
{ 
    var mvcHtmlString = System.Web.Mvc.Html.InputExtensions.TextBoxFor(htmlHelper, expression, htmlAttributes ?? new { @class = "text-box single-line date-picker" }); 
    var xDoc = XDocument.Parse(mvcHtmlString.ToHtmlString()); 
    var xElement = xDoc.Element("input"); 
    if (xElement != null) 
    { 
    var valueAttribute = xElement.Attribute("value"); 
    if (valueAttribute != null) 
    { 
     valueAttribute.Value = DateTime.Parse(valueAttribute.Value).ToShortDateString(); 
     if (valueAttribute.Value == "1/1/0001") 
     valueAttribute.Value = string.Empty; 
    } 
    } 
    return new MvcHtmlString(xDoc.ToString()); 
} 

而對於那些想知道JQuery的語法,以查找與date-picker類裝飾對象然後渲染日曆,那就是:

$(document).ready(function() { 
    $('.date-picker').datepicker({ inline: true, maxDate: 0, changeMonth: true, changeYear: true }); 
    $('.date-picker').datepicker('option', 'showAnim', 'slideDown'); 
}); 

希望這可以幫助別人的未來有這種需求。