在哪里可以找到要在 HTML Helper 或 EditorTemplate 中使用的 RemoteValidation 详细信息?
我目前有一个 MVC3 站点,在其中显示具有特定格式的日期时间字段,即大写 (DD-MON-YYYY)。
我为此创建了以下 DateTime.ascx EditorTemplate,并且添加了一个自定义数据日期选择器属性,该属性由某些自定义 javascript 拾取以将 Jquery UI Datepicker 添加到该字段。 <%@ 控制语言="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<input id="<%= ViewData.ModelMetadata.PropertyName %>"
name="<%= ViewData.ModelMetadata.PropertyName %>"
type="text"
value="<%= ViewData.TemplateInfo.FormattedModelValue.ToString().ToUpper() %>"
data-datepicker="true" />
我遇到的问题是,当我使用 EditorTemplate 时,我丢失了输入框中的所有 RemoteValidation 属性。显然是因为我没有专门添加它们!
有人可以指出我可以在模型或模型元数据中找到所需信息的正确方向,以便我可以使用远程验证和 JQueryUI 日期选择器构建输入框吗?
这是我当前从 EditorTemplate 得到的结果:
<input id="MyDate"
name="MyDate"
type="text"
value="19-SEP-2011"
data-datepicker="true" />
这就是我最终想要的:
<input class="text-box single-line"
data-val="true"
data-val-remote="&#39;Date (DD-MON-YYYY)&#39; is invalid."
data-val-remote-additionalfields="*.MyDate"
data-val-remote-url"=/Validation/IsDateValid"
data-val-required="A Date must be given"
id="MyDate"
name="MyDate"
type="text"
value="19-Sep-2011"
data-datepicker="true" />
有几件事需要注意:
- 如果需要,我愿意使用 HTMLHelper。我不限于 EditorTemplate,
- 我在控制器上有不同的 RemoteValidation 操作,用于不同的日期。所以我不能只是硬编码验证网址。我需要从模型中字段上修饰的 RemoteAttribute 中查找值。
- 同样,data-val-remote 值也需要根据模型字段上的 RemoteAttribute 进行加载。
有人有什么想法吗?
I currently have a MVC3 site in which I am displaying DateTime fields with a specific format, namely uppercase (DD-MON-YYYY).
I have create tthe following DateTime.ascx EditorTemplate for this, and I am adding a custom data-datepicker attribute whih gets picked up by some custom javascript to add a Jquery UI Datepicker to the field.
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<input id="<%= ViewData.ModelMetadata.PropertyName %>"
name="<%= ViewData.ModelMetadata.PropertyName %>"
type="text"
value="<%= ViewData.TemplateInfo.FormattedModelValue.ToString().ToUpper() %>"
data-datepicker="true" />
The problem I have is that when I use the EditorTemplate, I am losing all of the RemoteValidation attributes on the input box. Obviously because I'm not specifically adding them!
Can some one point me in the right direction of where i can find the required information within the Model or Model Metadata, so that I can build the input box with the Remote Validation and JQueryUI date picker?
This is what I get from the EditorTemplate currently:
<input id="MyDate"
name="MyDate"
type="text"
value="19-SEP-2011"
data-datepicker="true" />
This is what I want in the end:
<input class="text-box single-line"
data-val="true"
data-val-remote="'Date (DD-MON-YYYY)' is invalid."
data-val-remote-additionalfields="*.MyDate"
data-val-remote-url"=/Validation/IsDateValid"
data-val-required="A Date must be given"
id="MyDate"
name="MyDate"
type="text"
value="19-Sep-2011"
data-datepicker="true" />
A couple of things to note:
- I'm open to using a HTMLHelper if required. I'm not limited to a EditorTemplate
- I have different RemoteValidation actions on the controller that are used for different dates. So I can't just hardcode the validation url. I need to lookup the value fromt eh RemoteAttribute decorated on the field in the model.
- Likewise, the data-val-remote value also needs loading based on teh RemoteAttribute on the model field.
Does anyone have any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用助手来获取不显眼的验证脚本使用的 HTML5 data-* 属性:
You need to use a helper in order to get the HTML5 data-* attributes used by the unobtrusive validation script: