Problem with DateTime editor template (solved)

Nov 7, 2011 at 6:00 PM

I have a simple editor template (as seen in many examples) under Views\Shared\EditorTemplates\DateTime.cshtml that looks like this...

@model DateTime?

@Html.TextBox(

"Date", (Model.HasValue ? Model.Value.ToShortDateString() : string.Empty), new { @class = "datePicker"  }) 

 

I need some help generating this... If I use @* Generator : Template *@ and reference RazorGenerator.Templating, it will create a class based on RazorGenerator.Templating.RazorTemplateBase; however, this results in model, Html and Model being out of context, even if I include @using System.Web.Mvc, @using System.Web.Mvc.Html. This is due to the fact that the template does not derive from WebViewPage<DateTime?>.

It should be a @* Generator : Template *@ right? Can we use editor templates like this with the generator?

I was trying to follow the advice given here... http://geekswithblogs.net/michelotti/archive/2010/02/05/mvc-2-editor-template-with-datetime.aspx



Coordinator
Nov 7, 2011 at 6:05 PM

No, it probably shouldn't use Generator: Template, which is for simple (non-ASP.NET related) templates (see this post for some details). Have you tried with the default generator? I don't think we've tried this with EditorTemplates, so it's possible there are some issues.

Nov 7, 2011 at 6:14 PM

Yes, I didn't post it but I've tried with the default, which gives me the following generated class and compile error... Then of course we have a problem with the name which I suspect has to be DateTime in order for the MVC conventions to work (ie. substituting the editor for DateTime model properties).

generated.cs:

     [System.CodeDom.Compiler.GeneratedCodeAttribute("RazorGenerator", "1.2.0.0")]
    [System.Web.WebPages.PageVirtualPathAttribute("~/Areas/ModuleA/Views/Shared/EditorTemplates/DateTime.cshtml")]
    public class DateTime : System.Web.Mvc.WebViewPage<DateTime? >
    {
        public DateTime()
        {
        }...

Error:

    The type 'Proj.Areas.ModuleA.Views.Shared.EditorTemplates.DateTime' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>'

 

 

 


Coordinator
Nov 7, 2011 at 8:11 PM

Does fully qualifying your model help?

@model System.DateTime?

Nov 7, 2011 at 10:43 PM
Edited Nov 7, 2011 at 10:53 PM

That's it! Works on my machine :)

For the record, I was able to rename the template to ShortDateTime.cshtml and then use a [UIHint("ShortDateTime")] attribute on the model property to get the same behavior. But this way seems to be more along the line of convention.

For the next reader, here is the editor template code.

@model System.DateTime?
@Html.TextBox("", (Model.HasValue ? Model.Value.ToString("M/d/yyyy") : string.Empty), new { type = "date" })

Thanks for the advice, I never would have tried that on my own.

Feb 13, 2012 at 9:30 PM

So, couldn't (and shouldn't) razorgenerator detect when it is processing DisplayTemplates and EditorTemplates and ensure that the base classes generated are disambiguated between the model types underlying them and the class name for the generated c# file?  This is going to be a common conflict due to the expectations of the MVC convention (display/editor template name matches the model class name) and seems like something that RG could solve automagically.  Files that would otherwise compile just fine in MVC fail mysteriously when using RazorGenerator.  

Yeah, you can fully-qualify the model types manually, but couldn't RG do it for you?

-Jason