Question

I have an editor template for a custom object. Pretty basic - just has a bunch of strings and dates:

<%:Html.LabelFor(model => model.AString)%>
<%:Html.TextBoxFor(model => model.AString)%>

<%:Html.LabelFor(model => model.ADate)%>
<%:Html.EditorFor(model => model.ADate)%>

<%:Html.LabelFor(model => model.AnotherDate)%>
<%:String.Format("{0:d}", Model.AnotherDate)%>

This is stored in the Shared/EditorTemplates folder. I also have a DateTime.ascx editor template in the same location, to override ALL datetime fields.

As you can see in the code above, one of the date fields uses an EditorFor for the date field, the other does not. However, they both render the same, which leads me to believe the DateTime editor is not being accessed. Is there anything I'm doing wrong? Seems like this should be possible to do.

I've used the DateTime editor directly in a view, and it works fine.

Thanks for any help or insight.

Was it helpful?

Solution 2

I figured out the problem:

I have partial metadata classes set up for Data Annotations in the Model (I'm using entity framework). My partial Date view is for ?DateTime (DateTime.ascx)

I had the DataType attribute for all the dates set to [DataType(DataType.Date)] instead of [DataType(DataType.DateTime)].

Changing that attribute fixed it all up.

OTHER TIPS

I've tried your scenario under MVC 2 and MVC 3 and under MVC 3 it works as you expect.

Just so I understand correctly you have EditorTemplates in your Shared which contains your ComplexModel.ascx editor and within your ComplexModel.ascx you call Html.EditorFor(m => m.ADate).

I'm not sure about updates to MVC 2 with regards to this but MVC 3 it definitely works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top