我有一个aps.net MVC项目,我必须在用户端设置日期输入的范围,我是用jquery脚本手动完成的。今天发现html 5属性min和max,它们在限制日期值下工作得很好,错误显示在@Html.ValidationMessageFor
元素中,但唯一的麻烦是 - 我无法将其本地化。
代码我现在有我的日期字段:
<td>@Html.EditorFor(m => m.EndDate, new { htmlAttributes = new { @class = datepicker" ", id = "EditorForTillDate", max@="20.12.2018" } })</td>
<td style="width:10%">@Html.ValidationMessageFor(m => m.EndDate, "", new { @class = "text-danger" })</td>
和型号:
[Display(Name = "end_date", ResourceType = typeof(Localization))]
[DateWorkExpAttribute(ErrorMessageResourceType = typeof(Localization), ErrorMessageResourceName = "error_end_date_invalid")]
public Nullable<System.DateTime> EndDate { get; set; }
甚至可能吗?
答案 0 :(得分:0)
在ValidationMessageFor
中,在您的模型中添加Date annotations
时,您的代码将会有效:
[Display (Name = "end_date" ResourceType = typeof (Localization))]
[DateWorkExpAttribute (ErrorMessageResourceType = typeof (Localization), ErrorMessageResourceName = "error_end_date_invalid")]
[StringLength (255 MinimumLength = 3, ErrorMessage = "* Part descriptions must be between 3 and 255 characters in length.")]
[Required (ErrorMessage = "This value is empty !! It's not correctly")]
public Nullable <System.DateTime> EndDate {get; set; }
对于Html 5属性ValidationMessegeFor
将无效,因为它只是客户端站点的HTML 5属性。当控制器发布方法返回ValidationMassegeFor
时,ModelState.IsValid
将设置为false(这是服务器端的机制)。