How to get valid HTML attributes from htmlAttributes object when attribute "data- *"

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

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

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

Under the hood, this is what happens on the following issues:

How to get values ​​from an HtmlAttributes object

Passing an object into HTML attributes

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

And then Latter it will be called:

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

However, this will lead to the conclusion:

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

What can be done to produce valid HTML?

UPDATE Thanks to Zabavsky ...

 public virtual void HtmlAttributes(object htmlAttributes) { this.Attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes); } 
+4
source share
1 answer

HtmlHelper class has an AnonymousObjectToHtmlAttributes method that helps create HTML5 compatible markup. The method replaces underscores with a hyphen.

+5
source

Source: https://habr.com/ru/post/1482154/


All Articles