I was working on HtmlHelper.AnonymousObjectToHtmlAttributes.
It works well with anonymous object:
var test = new {@class = "aaa", placeholder = "bbb"};
var parseTest= HtmlHelper.AnonymousObjectToHtmlAttributes(test);
The result parseTest has two key-value pairs.
But for Dictionary object:
var attrsInDict = new Dictionary<string,object>() {
{"class", "form-control"},
{"placeholder", "Select one..."}
};
var attrs = HtmlHelper.AnonymousObjectToHtmlAttributes(attrsInDict );
The attrs obtained is a strange object, with 4 Keys and 4 Values. The 4 keys are Comparer, Count, Keys,Values.
Some other SO post asking about the difference between the two(here). The selected answer says
There's not too much difference...
Really? And what is the correct way to parse the attrsInDict and get the same result as the one we get from an anonymous object?
For, I intend to merge attribues in the following code:
var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
foreach (var item in attrs)
{
if (attr.ContainsKey(item.Key))
{
attr[item.Key] = $"{attr[item.Key]} {item.Value}";
}
else
{
attr.Add(item.Key, item.Value);
}
}