Пример #1
0
        /// <summary>
        /// Renders the filter item.
        /// </summary>
        public async Task <IViewComponentResult> InvokeAsync(string url, ListRequestVM request, string propName, string title)
        {
            if (request == null || propName == null || title == null)
            {
                throw new ArgumentNullException();
            }

            var prop = request.GetType().GetProperty(propName);

            if (prop == null)
            {
                throw new ArgumentException($"Request of type '{request.GetType().Name}' does not have a property '{propName}'.");
            }

            var cloneRequest = ListRequestVM.Clone(request);

            prop.SetValue(cloneRequest, prop.PropertyType.IsValueType ? Activator.CreateInstance(prop.PropertyType) : null);

            var vm = new ListItemFilterVM
            {
                Title     = title,
                CancelUrl = ListRequestHelper.GetUrl(url, cloneRequest)
            };

            return(View("~/Areas/Admin/Views/Components/ListItemFilter.cshtml", vm));
        }
Пример #2
0
        /// <summary>
        /// Gets all arbitrary values for the request.
        /// </summary>
        public static IEnumerable <KeyValuePair <string, string> > GetValues(ListRequestVM vm)
        {
            var dict = new List <KeyValuePair <string, string> >();

            if (vm == null)
            {
                return(dict);
            }

            var props = vm.GetType().GetProperties();

            foreach (var prop in props)
            {
                var propType = prop.PropertyType;
                var value    = prop.GetValue(vm);

                if (value == null)
                {
                    continue;
                }

                var defValue = propType.IsValueType && Nullable.GetUnderlyingType(propType) == null
                    ? Activator.CreateInstance(propType)
                    : null;

                if (value.Equals(defValue))
                {
                    continue;
                }

                var isEnumerable = propType.IsArray ||
                                   (propType != typeof(string) && propType.GetInterfaces().Contains(typeof(IEnumerable)));
                if (isEnumerable)
                {
                    foreach (object elem in (dynamic)value)
                    {
                        Add(prop.Name, elem);
                    }
                }
                else
                {
                    Add(prop.Name, value);
                }
            }

            return(dict);

            void Add(string propName, object value)
            {
                var str = value is IConvertible fmt
                    ? fmt.ToInvariantString()
                    : value.ToString();

                dict.Add(new KeyValuePair <string, string>(propName, str));
            }
        }
Пример #3
0
        /// <summary>
        /// Renders the result.
        /// </summary>
        public async Task <IViewComponentResult> InvokeAsync(ListRequestVM request, string propName)
        {
            if (request == null || propName == null)
            {
                throw new ArgumentNullException();
            }

            var prop = request.GetType().GetProperty(propName);

            if (prop == null)
            {
                throw new ArgumentException($"Request of type '{request.GetType().Name}' does not have a property '{propName}'.");
            }

            if (!prop.PropertyType.IsArray)
            {
                throw new ArgumentException($"Property {request.GetType().Name}.{propName} must be an array.");
            }

            var elemType = prop.PropertyType.GetElementType();

            if (!elemType.IsEnum)
            {
                throw new ArgumentException($"Type '{prop.PropertyType.Name}' is not an enum.");
            }

            var enumValues = GetEnumValues(elemType);
            var selected   = prop.GetValue(request) as int[];
            var result     = new List <ListEnumFilterItemVM>();

            foreach (var enumValue in enumValues)
            {
                result.Add(new ListEnumFilterItemVM
                {
                    PropertyName = propName,
                    Title        = enumValue.Value,
                    Value        = enumValue.Key.ToInvariantString(),
                    IsActive     = selected?.Any(x => x == enumValue.Key) == true
                });
            }

            return(View("~/Areas/Admin/Views/Components/ListEnumFilter.cshtml", result));
        }