我有一个编辑器模板如下,但 class、maxlength 和 size 属性未获取源代码。
@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" })
Source
<input class="text-box single-line" id="Criteria_FromDate" name="Criteria.FromDate" type="text" value="" />
我怎样才能解决这个问题?
确保您的编辑器模板已命名DateTime- 放置在文件夹 Views/Shared/EditorTemplates 中,您的模型 (Criteria.FormDate) 是同类型作为 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 用于传递的模型。当您有相同模型类型的多个编辑器时,就会出现这种情况,因此您显式调用它们(就像调用部分视图并将模型传递给它一样)。
EDIT:查看您的模板后,在我看来您的 Criteria.FormDate 不可为空。您应该考虑改进/重构模板中的代码。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)