1

我有一个如下的编辑器模板,但类、最大长度和大小属性没有到达源代码。

@using System.Globalization
@model DateTime?
@Html.TextBox("", (Model != null && Model.HasValue && !Model.Value.ToString(CultureInfo.InvariantCulture).Contains("1900") && !Model.Value.ToString(CultureInfo.InvariantCulture).Contains("0001") ? Model.Value.ToString("MM/dd/yyyy") : string.Empty), new { @class = "datePicker", maxlength = "12", size = "12" })

我已将其更改为关注,它仍然是一样的

@Html.EditorFor(x => x.Criteria.FromDate, new { @class = "datePicker", maxlength = "12", size = "12" })

资源

<input class="text-box single-line" id="Criteria_FromDate" name="Criteria.FromDate" type="text" value="" />

我怎样才能解决这个问题?

4

1 回答 1

1

确保您的编辑器模板命名为DateTime - 放置在文件夹 Views/Shared/EditorTemplates 中,并且您的模型 (Criteria.FormDate)与EditorTemplate 模型 ( DateTime? ) 的类型相同

如果所有 DateTime 字段都具有相同的最大长度和大小,您可以将它们硬编码在 EditorTemplate 中。您的 html 示例:

@EditorFor(x => x.Criteria.FormDate) //no need to pass html attributes object if they are not used in the editor template

- 值得尝试@EditorFor(model, "EditorTemplateName")明确地说您希望 TemplateEditor 用于传递的模型。当您有多个相同模型类型的编辑器时就是这种情况,因此您显式调用它们(就像调用局部视图并将模型传递给它一样)。

编辑: 查看您的模板后,在我看来您的 Criteria.FormDate 是不可为空的。您应该考虑改进/重构模板中的代码。

于 2012-08-21T10:17:32.637 回答