protected override void OnParametersSet() { if (AdditionalAttributes == null) { AdditionalAttributes = new Dictionary <string, object>(); } if (!AdditionalAttributes.ContainsKey("class")) { //check if have a table css class on the model send DisplayTableAttribute cssClass = typeof(T).GetCustomAttribute <DisplayTableAttribute>(); if (cssClass != null && cssClass.TableClass != null) { AdditionalAttributes.Add("class", cssClass.TableClass); } else { AdditionalAttributes.Add("class", DefaultCSSClass); } } if (Items != null) { StringBuilder html = new StringBuilder(); PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); if (properties.Length > 2) { foreach (T item in Items) { } } } }
protected override void OnParametersSet() { if (Items != null) { if (AdditionalAttributes == null) { AdditionalAttributes = new Dictionary <string, object>(); } if (!AdditionalAttributes.ContainsKey("class")) { //check if have a table css class on the model send DisplayTableAttribute cssClass = typeof(T).GetCustomAttribute <DisplayTableAttribute>(); if (cssClass != null && cssClass.TableClass != null) { AdditionalAttributes.Add("class", cssClass.TableClass); } else { AdditionalAttributes.Add("class", DefaultCSSClass); } } PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | //get public names BindingFlags.Instance); //get instance names //get all my attributes DisplayTableAttribute[] attributes = new DisplayTableAttribute[properties.Length]; StringBuilder html; if (Head == null) { html = new StringBuilder(); for (int i = 0; i < properties.Length; i++) { attributes[i] = properties[i].GetCustomAttribute <DisplayTableAttribute>(); //get if my custom attributes //custom header class string OpenTHTag = attributes[i] != null && attributes[i].HeaderClass != null ? $"<th class=\"{attributes[i].HeaderClass}\">" : "<th>"; //custom header name Attribute alias = Attribute.GetCustomAttribute(properties[i], typeof(DisplayAttribute)); //get if have attribute display to change the name of the property string header = attributes[i] != null && attributes[i].Header != null ? attributes[i].Header : //custom header name alias == null ? properties[i].Name : ((DisplayAttribute)alias).GetName(); //if not get the display attribute or name html.Append($"{OpenTHTag}{header}</th>"); } DefaultHead = new MarkupString(html.ToString()); } if (Body == null) { html = new StringBuilder(); //get all the item to show the values foreach (T item in Items) { html.Append("<tr>"); //show the values for (int i = 0; i < properties.Length; i++) { attributes[i] = properties[i].GetCustomAttribute <DisplayTableAttribute>(); //get if my custom attributes string OpenTDTag = attributes[i] != null && attributes[i].ColClass != null ? $"<td class=\"{attributes[i].ColClass}\">" : "<td>"; var value = attributes[i] != null && attributes[i].ValueFormat != null?string.Format(attributes[i].ValueFormat, properties[i].GetValue(item)) : properties[i].GetValue(item); html.Append($"{OpenTDTag}{value}</td>"); } html.Append("</tr>"); } DefaultBody = new MarkupString(html.ToString()); } } else { return; } }