2

I know I can add custom attributes to any given helper using an anonymous type with the attribute and value specified for it to be rendered as a HTML5 attribute however im looking to achieve the same across all HTML Helpers in a given view triggered by an externally specified helper. Similar to the same functionality you receive from the un-obtrusive JavaScript helper where it renders validation rules in the context of a form field's attributes.

Does anyone know if there is a "simple" way to inject these customisations into the helpers, or will I need to extend each of the helpers independently?

Cheers

4
  • can you give a concrete example of what you want? You may be able to use Templated Razor Delegates (haacked.com/archive/2011/02/27/templated-razor-delegates.aspx). Commented Jun 7, 2011 at 16:47
  • We're looking to do some work on the client side depending on a form's field being changed. Our desired approach was to add a custom attribute with the original field's value then onChange do a compare on the new value and the old.. meaning someone can return a field to its original state and it not be considered changed. So the idea was, each helper (textbox, select input etc) would render the attributes automatically. Commented Jun 8, 2011 at 13:26
  • instead of using a custom attribute, it would be easier to use jQuery to go through each field and store the original values when the document is ready. You then can check the field's original value when a field is changed, and take your actions accordingly. Commented Jun 8, 2011 at 21:03
  • @councellorben, that was my original solution which was declined by my boss was was looking for a plan b. Commented Jun 10, 2011 at 17:39

1 Answer 1

1

You can't extend all methods from one centralized point (write code that will extend all your html helper methods by adding overload with additional 'htmlAttributes' parameter - may be it is possible by using IL methods generation, but it is hard way).

Each extension should be overload of your html helper method, and you can implement like in example:

public static class HtmlExtensions
{
    public static string MyPager(this HtmlHelper html, string parameter1, int parameter2)
    {
        var builder = new TagBuilder("div");
        GenerateMyPagerBody(builder , parameter1, parameter2); // insert body into tag
        return builder.ToString(TagRenderMode.SelfClosing);
    }

    public static string MyPager(this HtmlHelper html, string parameter1, int parameter2, object htmlAttributes)
    {
        var builder = new TagBuilder("div");
        GenerateMyPagerBody(builder , parameter1, parameter2);
        builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
        return builder.ToString(TagRenderMode.SelfClosing);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.