/// <summary> /// Returns the URL for current request. /// </summary> public static string GetUrl(string url, ListRequestVM request) { var args = GetValues(request); var strArgs = args.Select(x => HttpUtility.UrlEncode(x.Key) + "=" + HttpUtility.UrlEncode(x.Value)); return(url + "?" + string.Join("&", strArgs)); }
/// <summary> /// Renders the pagination control. /// </summary> public async Task <IViewComponentResult> InvokeAsync(string url, ListRequestVM request, int pageCount) { var result = new List <ListPaginatorPageVM>(); var pages = GetPageNumbers(request.Page, pageCount); var prevPage = (int?)null; foreach (var page in pages) { if (prevPage != null && prevPage != page - 1) { result.Add(new ListPaginatorPageVM { Title = "..." }); } var clone = ListRequestVM.Clone(request); clone.Page = page; var cloneUrl = ListRequestHelper.GetUrl(url, clone); result.Add(new ListPaginatorPageVM { Url = cloneUrl, Title = (page + 1).ToString(), IsCurrent = page == request.Page }); prevPage = page; } return(View("~/Areas/Admin/Views/Components/ListPaginator.cshtml", result)); }
/// <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)); }
/// <summary> /// Renders the sortable header. /// </summary> public async Task <IViewComponentResult> InvokeAsync(string url, ListRequestVM request, string propName, string title) { var vm = new ListHeaderVM { Title = title }; var cloneRequest = ListRequestVM.Clone(request); cloneRequest.Page = 0; if (cloneRequest.OrderBy == propName) { cloneRequest.OrderDescending = !cloneRequest.OrderDescending; vm.IsDescending = cloneRequest.OrderDescending; } else { cloneRequest.OrderBy = propName; cloneRequest.OrderDescending = false; } vm.Url = ListRequestHelper.GetUrl(url, cloneRequest); return(View("~/Areas/Admin/Views/Components/ListHeader.cshtml", vm)); }
/// <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)); } }
/// <summary> /// Renders the included fields. /// </summary> public async Task <IViewComponentResult> InvokeAsync(ListRequestVM request, string[] include = null) { var values = ListRequestHelper.GetValues(request) .Where(x => x.Key == nameof(ListRequestVM.OrderBy) || x.Key == nameof(ListRequestVM.OrderDescending) || include?.Contains(x.Key) == true) .ToList(); return(View("~/Areas/Admin/Views/Components/ListHiddenFilter.cshtml", values)); }
/// <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)); }