4

I would like to pass htmlAttributes as an object to a method like so...

     foo.HtmlAttributes(new { data_bind = "foo"});

In all the MVC HtmlHelpers I have used the underscore as a hyphen, this would output valid html "data-bind"

Under the hood this is what is going on as per the following questions:

How to get values out of object HtmlAttributes

Passing an object to HTML attributes

    public virtual void HtmlAttributes(object htmlAttributes)
    {
       this.Attributes = new RouteValueDictionary(htmlAttributes);
    }

And then Latter this will be called:

    internal virtual void ApplyConfiguration(TagBuilder tag)
    {
            tag.MergeAttributes(this.Attributes);
    }

However this would output:

<div data_bind="foo"></div>

What can I do to output valid HTML?

UPDATE Thanks to Zabavsky...

public virtual void HtmlAttributes(object htmlAttributes)
{      
        this.Attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
}
2

1 Answer 1

6

HtmlHelper class has AnonymousObjectToHtmlAttributes method, which helps to create markup that is compliant with HTML5. The method replaces underscore characters with hyphens.

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.