public static void FillGenericTable(ref GenericTableVModel table, IEnumerable collection, Type metaData, GenericTableSetting setting) { List <GenericTableRowVmodel> rows = new List <GenericTableRowVmodel>(); foreach (var item in collection) { ModelMetadata itemMetadata = ModelMetadataProviders.Current .GetMetadataForType(() => item, metaData); GenericTableRowVmodel row = new GenericTableRowVmodel(); row.Setting = setting; List <GenericTableCellVmodel> cells = new List <GenericTableCellVmodel>(); foreach (ModelMetadata property in itemMetadata.Properties) { if (property.IsComplexType) { if (typeof(IEnumerable).IsAssignableFrom(property.ModelType)) { // Its a collection within the collection so ignore. continue; } } GenericTableCellVmodel cell = new GenericTableCellVmodel(); if (property.PropertyName.Equals(setting.Id)) { row.Id = GetValueDisplayControl(property); } //row.Id = property.PropertyName.Equals(setting.ForceToId) ? GetValueDisplayControl(property) : row.Id; string[] excludedProp = setting.PropertiesNotToShow.Split(','); if (!excludedProp.Contains(property.PropertyName)) { cell.Name = !string.IsNullOrEmpty(property.DisplayName) ? property.DisplayName : property.PropertyName; cell.Value = GetValueDisplayControl(property); cells.Add(cell); } } row.Cells = cells; rows.Add(row); } table.Setting = setting; table.Rows = rows; }
private static string CreateTableString(string header, string body, GenericTableVModel model) { StringBuilder html = new StringBuilder(); System.Web.Mvc.TagBuilder table = new System.Web.Mvc.TagBuilder("table"); table.MergeAttribute("class", model.Setting.Class); table.MergeAttribute("id", model.Setting.Id); table.InnerHtml = header + body; html.Append(table.ToString()); return(html.ToString()); }
public static MvcHtmlString jDisplayTable(this HtmlHelper helper, GenericTableSetting setting) { // Get the model metadata ModelMetadata metaData = helper.ViewData.ModelMetadata; // Get the fully qualified name of the property string fieldName = ExpressionHelper.GetExpressionText(helper.ViewData.Model.GetType().FullName); // Get the collection to render in the table IEnumerable collection = metaData.Model as IEnumerable; if (collection == null) { throw new InvalidCastException(string.Format(_InvalidCollection, fieldName)); } // Get the type in the collection Type type = GetCollectionType(collection); if (type == null) { // TODO: Wahts the right exception? throw new InvalidCastException("The type in the collection could not be resolved"); } // Get the metadata of the type (in case there are no items in the table) ModelMetadata typeMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, type); var tableModel = new GenericTableVModel(); FillGenericTable(ref tableModel, collection, type, setting); var tHeader = helper.ViewContext.Controller.ControllerContext.RenderPartialView(@"_GenericTableHeader", tableModel.Rows.FirstOrDefault()); var tBody = helper.ViewContext.Controller.ControllerContext.RenderPartialView(@"_GenericTableBody", tableModel.Rows); var table = CreateTableString(tHeader, tBody, tableModel); return(MvcHtmlString.Create(table)); }