5

I have a ASP.NET MVC4 project with HTML5 semantic markup enabled. When using @Html.EditorFor(m => m.MyDateTimeField) the output is an input with type="datetime". I want to be able to stop EditorFor from generating HTML5 markup. How can I do this?

This is only a problem in Opera at the moment because other browsers (as far as I know) don't support type="datetime". I have a jQuery datepicker on the field so in Opera I get both the jQuery datepicker and the browser datepicker.

I can fix using any of:
a. Use js to change the input type
b. Use Html.TextBoxFor(m => m.MyDateTimeField)
c. Use a custom editor template
d. Use modernizr to detect if datetime is supported and if so, don't use jQuery datapicker

The solution I want is to disable HTML5 generation in HtmlHelper.EditorFor. I want HtmlHelper.EditorFor to function the same way it would if I hadn't ticked the "html5 semandtic markup" checkbox on project creation.

EDIT: After a bit more searching, I've come across the DataTypeAttribute which I can apply on my model fields to force them to be rendered as input type="text" rather than the html5 match. This is a potential fix, but it is less than ideal. Surely there must be a switch somewhere to turn off html5 in EditorFor (and equivalent helpers)?

A bit more info: I have two projects. The first one was created with "Html5 semantic markup" on, the other without. The first one uses html5 input types when using EditorFor, the second does not. I need to stop the first project EditorFor behaving the way is does without removing the EditorFor. There must be a setting somewhere?

4
  • I tried creating an MVC4 project in VS11RC and did not see the old HTML5 Semantic Markup checkbox. You can always right click your mvc proj, unload it, and open it in an editor to look for settings. I'd imagine this is where the setting would be. Commented Jul 31, 2012 at 14:27
  • See my trick for dealing with HTML5/jQuery in part 4 of my tutorial asp.net/mvc/tutorials/javascript/… The future is HTML5, not jQuery, so you should disable jQuery if HTML5 does the job. Commented Aug 19, 2012 at 1:14
  • @RickAndMSFT. If you reread my question, you'll notice I already stated (d) that this is a solution I am already aware of and DO NOT want to use. I want to tell my project to stop using the html5 templates. On your point about HTML5 being the future as opposed to jQuery, that's fine but now is the present. The current HTML5 input types don't look very nice and are not customisable to the extent of the third party equivalents. Commented Aug 19, 2012 at 9:21
  • @flem - did you read my tutorial? The logic lets you detect and remove MVC generated data- attributes - just modify the code. It has an example of removing the HTML5 attributes. Commented Aug 19, 2012 at 18:00

2 Answers 2

2
+100

The problem is in one of the project the package references are not updated properly and hence you are seeing the html5 semantic rendered.

This issue happens when you upgrade the ASP.NET MVC beta version to RC and there two ways you could solve the problem either updating all the packages through Nuget or manually add the reference to assemblies.

The issue and solution is clearly explained by Scott here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Mark. This was bothering me! Bounty makes all the difference.
2

That's really feature that got introduced and wasn't documented (nasty...). Basically ASP.NET MVC 4 now defines internal editor templates for Date and DateTime. In ASP.NET 3 there were no templates so I personally had an EditorTemplate (in Views\Shared\EditorTemplates) called DateTime.cshtml which was handling all DateTime types and dealing with DataTypes.Date vs DataTypes.DateTime. Now you need to override both templates. Nasty.

Edit: Basically if you just want to not touch any of your existing code define two templates Date.cshtml and DateTime.cshtml which both have @Html.TextBoxFor(model => model) in Views\Shared\EditorTemplates\

6 Comments

Thanks, but that's not the solution I'm looking for (as mentioned in my question). I want to be able to make EditorFor in the project that generates html5 inputs to behave the same as the project that generates html4 without writing code. There must be something that tells the compiler where to look for built in templates...
@flem AFAIK - there is no such option - their are built-in and you can't disable them through a switch in configuration - only override the built-in templates.
@flem also AFAIK semantic markup are the new html5 layout elements and has nothing to do with input types.
How do you explain it working in one project and not the other? What is the difference? One is using the none html5 built in templates and the other is using the html5 built in templates. How does the project know which set to use?
Are you sure that both projects are running on top of MVC 4, especially the one that you don't see html5 fields generated? If it was an mvc3 project and you have simply updated the assembly references to mvc4 you also need to update the web.config assembly versions. There is no "semantic html5" option for MVC4 projects. Also what it used to do is only use different layout html and reference modernizr.js - that's all. It had no influence on what editorfor generated.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.