Search code examples
asp.net-mvc-3editortemplates

Extending Asp.net MVC 3 Editor Templates


I have a model that contains a property for an image url:

[Required]
[DataType(DataType.ImageUrl)]
public string Logo { get; set; }

As you can see the data type is an ImageUrl and I've usesd this fact to create an editor template in Views/Shared directory.

The template only needs to extened the default template by adding a few extra html elements. I'm not sure how to go about this.

I've tried specifying the template name when I call the html helpers but that doesn't seem to work for me.

The code I'm using is (note that this is inside the custom template):

@Html.EditorFor(model => model, "string")

Incase you're wondering I got the template name by using reflector on the MVC 3 dll, then under

System.Web.Mvc.Html

I looked at the contructor for the TemplateHelpers class and saw this line:

dictionary3.Add(typeof(string).Name, new Func<HtmlHelper, string>(DefaultEditorTemplates.StringTemplate));

Cheers for any Help Tony

Update

Forgot to mention. The reason I'm doing this is to take advantage of the unobtrusive validation that's with MVC 3


Solution

  • Why not just use @Html.TextBoxFor(m => m) instead of EditorFor in your editor template? :)