Example #1
0
        public Model.ViewModel.GridVM GetGridViewModelForObject(IQueryable <object> collection, string objectId, string objectType, string bindingPath, CollectionViewModelParameters parameters)
        {
            var gv = new Model.ViewModel.GridVM();

            gv.PropertyValue = $"{collection.Cast<object>().FirstOrDefault()?.GetType().Name}[{collection.Count()}]";

            var firstElement = collection.Cast <object>().FirstOrDefault();

            if (firstElement == null)
            {
                return(gv);
            }

            var elementType           = firstElement.GetType();
            var elementIdPropertyName = dataService.GetIdPropertyName(elementType);
            var propertyInfos         = viewConfigurationService.GetGridCollumnInPropertyGrid(elementType, elementIdPropertyName);
            var idProperty            = elementType.GetProperty(elementIdPropertyName);

            bool isFirst = true;
            var  page    = collection.Skip(parameters.Page * parameters.PageSize).Take(parameters.PageSize);
            int  iRow    = 0;

            foreach (var element in page)
            {
                var row = new RowViewModel();
                row.Cells = new CellViewModel[propertyInfos.Length];
                var rowId = idProperty.GetValue(element);
                row.Id = rowId?.ToString();
                row.ElementTypeFullName = helper.GetTypeName(elementType);
                row.EditUrl             = helper.GetEditUrlForClass(row.Id, elementType);

                for (int i = 0; i < propertyInfos.Length; i++)
                {
                    var cellProperty     = propertyInfos[i];
                    var cellValue        = cellProperty.GetValue(element);
                    var cellPropertyType = cellProperty.PropertyType;
                    var cellType         = helper.CheckPropType(cellPropertyType);

                    if (isFirst)
                    {
                        var cvm = new ColumnViewModel()
                        {
                            BindingPath = cellProperty.Name, Header = cellProperty.Name
                        };
                        gv.Columns.Add(cvm);
                    }

                    var cell = new CellViewModel()
                    {
                        IsBigString = helper.IsBigString(cellValue, BaseService.EnumPreviewType.Grid),
                        Value       = helper.GetPreview(cellValue, BaseService.EnumPreviewType.Grid)
                    };
                    cell.MainObjectId   = objectId;
                    cell.MainObjectType = objectType;
                    cell.BindingPath    = $"{bindingPath}[{iRow}].{cellProperty.Name}";

                    row.Cells[i] = cell;

                    if (cellType == PropertyTypes.Class)
                    {
                        if (cellValue != null)
                        {
                            var valueType  = cellValue.GetType();
                            var idPropName = dataService.GetIdPropertyName(valueType);
                            var idProp     = valueType.GetProperty(idPropName);
                            var id         = idProp.GetValue(cellValue)?.ToString();

                            cell.EditUrl = helper.GetEditUrlForClass(id, valueType);
                        }
                    }
                    else if (cellType == PropertyTypes.Collection)
                    {
                        cell.EditUrl = helper.GetEditUrlForCollection(helper.GetCollectionElementType(cellPropertyType), rowId, elementType);
                    }
                }
                gv.Rows.Add(row);
                isFirst = false;
                iRow++;
            }

            return(gv);
        }
 public IViewComponentResult Invoke(Model.ViewModel.GridVM gridViewModel)
 {
     return(View(gridViewModel));
 }