The HtmlHelper object has a method that converts the object into a dictionary of names / values, which you can then combine into your tag as it is created. For example, this code will generate a <script> with any additional attributes:
var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes) as IDictionary<string, object>; TagBuilder tag = new TagBuilder("script"); tag.MergeAttributes(attributes); tag.MergeAttribute("type", "text/javascript"); tag.MergeAttribute("src", scriptPath);
You can either provide overloads or use the default values ββto provide a null value for htmlAttributes , which will lead to the creation of an empty Dictionary .
(This method also degrades attribute names in valid HTML attributes, etc., so it is safe to use for almost any object.)
Michael edenfield
source share