Creation of input HtmlHelpers (eg: input type=file)

725 Views Asked by At

We need validation attributes on and no HtmlHelper is provided in this respect.

So we started developing a new helper.

To find out how, we looked over the MS code by using JetBrains dotPeek and found everything we need in InputExtensions.cs from System.Web.Mvc (4.0).

There is a problem: for creating input helpers we need to use InputHelper() method which is unfortunately private.

Why is private? This should be a good extension point for input helpers.

So now we need to duplicate this code in our case to provide functionality for creation of with template ing, validation attributes, ....

Do you think that what we are doing is correct?

Is there a page in the documentation that says how to do extension methods of this kind?

P.S. found some implementations but they are not quite what I want: http://blog.isilverlabs.com/2011/01/mvc-file-input-helper/

1

There are 1 best solutions below

0
On

Custom field templates may be what you're looking for. They'll allow you to overwrite the html that is generated when you call @Html.EditorFor() or @Html.DisplayFor().

Mark the property on your view model with the UIHint attribute, providing your custom template name.

The HtmlHelper extension methods will automatically look for a matching template in the DisplayTemplates or EditorTemplates folder.

Here's a similar StackOverflow question: How to create custom editor/display templates in ASP.NET MVC 3?