views:

38

answers:

2

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.

A: 

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.

BuildStarted
Interesting - I don't suspect I'll be able to get MVC 3 anytime soon, unfortunately (I need approvals to install packages, and only recently was given permission for MVC 2). I'm curious about what is the difference between them that allows it work in MVC 3? Guess I'll have to come up with a workaround then.
morganpdx
Yeah, I wouldn't upgrade just yet anyway as MVC 3 is still in a preview state and not ready for production use. Without stepping through the code right now it's probably just the way the EditorFor initially loaded the templates and didn't recursively check for overriding templates...though I can't be sure off hand. Though it is definitely something I should look into :)
BuildStarted
Thanks for the info Build :)
morganpdx
A: 

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.

morganpdx