I'm upgrading an MVC3 application to MVC4 using the instructions from Microsoft. Everything went fairly smoothly - except a few of my date model properties are now rendering differently. For example, one of these properties is defined in the viewmodel like this:
[Required(ErrorMessage = "Required")]
[DataType(DataType.Date)]
[RegularExpression(@"([1-9]|0[1-9]|1[012])...",
ErrorMessage = "Format is mm/dd/yyyy")]
[FormatHint("mm/dd/yyyy")]
[InputSize("small")]
public string Date { get; set; }
Before upgrading to MVC4, this would be rendered via calling @Html.EditorFor(m => m.Date)
which would use a custom EditorTemplate - the String.cshtml
template (since it's a string!). I have some custom data annotations that formats the html so it utilizes a field layout, jQueryUI, and twitter Bootstrap on the client side. The validation is done via jquery validation unobtrusive. Anyhow, this is how it previously rendered:
Now that I'm using MVC4, the String.cshtml
editor template is not being called for this property any longer. It renders like this (in Chrome using the HTML5 editor stuff, I assume):
The input
element looks pretty much the same - all the jQuery validation bits are in there - the only difference seems to be the type
attribute is now type="date"
, where before it was type="text"
.
I'd like to continue using the String.cshtml
EditorTemplate for this datatype. I'm thinking there might be a data annotation that I can put on the ViewModel property to provide a TemplateHint for @Html.EditorFor(...)
. If not this, I'd like to know the custom EditorTemplate that I can write to hijack MVC4's formatting (I've already tried DateTime.cshtml
- it's not being called either). If not either of those, then I'm open to suggestions on how to get my property rendering like it used to.
In MVC4
, the used template is determinated from :
UIHintAttribute
if anyDataTypeAttribute
if anyIn MVC3
, the DataTypeAttribute
was not used.
Your property has a
[DataType(DataType.Date)]
so the template Date.cshtml
is used. If it does not exists, default rendering is executed.
You have two options to resolve your problem :
UIHint("String")
on your property, orString.cshtml
to Date.cshtml
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With