2

This is a rather interesting issue that was quite hard to spot...and I cannot resolve it. In a nutshell, I have the following in a view trying to render a date field (ignore archiac syntax - it's an old project);

        <%: Html.TextBoxFor(model => model.DateOfRenewal, 
                new { 
                    type = "date",
                    placeholder = "DD/MM/YYYY",
                    Value = (Model.DateOfRenewal != null) ? Model.DateOfRenewal.Value.ToString("yyyy-MM-dd") : "" 
                })%>

Now on the model, the date format attribute is set to "dd/MM/yyyy" appropriately. We're rendering to "yyyy-MM-dd" when generating the HTML in order for a jquery library (webshim polyfiller) to pick up the date correctly when it starts running against the field. I don't have control over this library so I'm stuck with it and I have control only over the MVC side of things.

What's happening is that the field above looks ok when you inspect it but viewing the source shows the following;

<input Value="2008-05-12" data-val="true" data-val-date="The field Renewal Date must be a date." 
       id="Ratings_DateOfRenewal" 
       name="Ratings.DateOfRenewal" placeholder="DD/MM/YYYY" 
       type="date" 
       value="12/05/2008 00:00:00" />

Notice that Value and value are both present. The lower case version is what goes in there as part of TextBoxFor and the uppercase Value is what appears in my custom attribute when specifying in the view.

However, changing the custom attribute in the view from an uppercase to a lower case is runs fine but it does not have the custom date format. As a result, I get a blank field as webshim cannot interpret it.

Is there anyway to get a custom date format without;

  • Changing the model attribute (required for server side validation)
  • Changing/modifying the jquery library

In a nutshell, is there a way to get the custom formatted date as a single, lowercase value html attribute when rendered?

EDIT The marked answer below changes the lowercase value to the specified format. To fulfill the original request, the MVC markup has to change to a lowercase value as well. This fixes the issue.

EDIT2 The duplicate mark is invalid. While the situation is very close, none of the answers in the linked post work in this situation - inherently marking it as different.

DiskJunky
  • 4,750
  • 3
  • 37
  • 66
  • you shouldn't need to specify the value. – Daniel A. White Feb 23 '15 at 20:04
  • I'm open to suggestions on getting the date format to "yyyy-MM-dd" – DiskJunky Feb 23 '15 at 20:06
  • @DanielA.White, not a duplicate. I've already checked quite a few posts on SO before posting and had already come across that one – DiskJunky Feb 23 '15 at 20:08
  • 1
    from the link daniel posted there is an answer for MVC4 and up which seems to be a reasonable solution. http://stackoverflow.com/a/13702321/372529 – phillip Feb 23 '15 at 20:12
  • 1
    @DanielA.White: close, but not quite the same situation, and none of the answers address the OP's problem – Chris Pratt Feb 23 '15 at 20:18
  • @phillip that worked, actually (somewhat to my surprise, I thought that would affect accepted input values on the POST), thank you! I'll mark as accepted answer if posted below – DiskJunky Feb 23 '15 at 20:21

0 Answers0