0

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="&amp;#39;Date (DD-MON-YYYY)&amp;#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?

matino
  • 17,199
  • 8
  • 49
  • 58
Nick
  • 1,116
  • 2
  • 12
  • 24

1 Answers1

1

You need to use a helper in order to get the HTML5 data-* attributes used by the unobtrusive validation script:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.TextBox(
    "", 
    ViewData.TemplateInfo.FormattedModelValue.ToString().ToUpper(), 
    new { data_datepicker = "true" }
) %>
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928