How to get values out of object HtmlAttributes

前端 未结 3 836
一生所求
一生所求 2021-02-02 10:49

In asp.net mvc I always see the built in html helpers they always have object htmlAttirbutes.

Then I usually do new {@id = \"test\", @class=\"myClass\"}.

How do

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-02 11:49

    you can transform the object htmlAttirbutes to an attribute/value string representation like this :

    var htmlAttributes = new { id="myid", @class="myclass" };
    
    string string_htmlAttributes = "";
    foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(htmlAttributes))
    {
      string_htmlAttributes += string.Format("{0}=\"{1}\" ", property.Name.Replace('_', '-'), HttpUtility.HtmlAttributeEncode(property.GetValue(htmlAttributes).ToString()));
    }
    

    PropertyDescriptor belong to the class System.ComponentModel

提交回复
热议问题