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="&#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" />
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?