public async Task <IEnumerable <SelectListItem> > GetSelectListAsync(SelectListContext context)
        {
            var items = await GetSelectListItemsAsync(context);

            if (items == null)
            {
                return(null);
            }

            var itemList = items.ToList();

            if (SetSelectedAutomatically)
            {
                if (context.CurrentValues != null)
                {
                    foreach (var item in itemList)
                    {
                        var value = item.Value ?? item.Text;
                        item.Selected = context.CurrentValues.Contains(value);
                    }
                }
            }

            if (context.SelectedOnly)
            {
                itemList.RemoveAll(item => !item.Selected);
            }

            if ((context.ModelExplorer.Metadata.IsNullableValueType && Nullable) || Nullable)
            {
                if (!context.SelectedOnly)
                {
                    itemList.Insert(0, new SelectListItem {
                        Text = "", Value = "", Selected = SetSelectedAutomatically ? (context.CurrentValues == null || context.CurrentValues.Count == 0) : false
                    });
                }
                else if (itemList.Count == 0)
                {
                    itemList.Insert(0, new SelectListItem {
                        Text = "", Value = "", Selected = true
                    });
                }
            }

            return(itemList);
        }
 protected override Task <IEnumerable <SelectListItem> > GetSelectListItemsAsync(SelectListContext context)
 {
     return(Task.FromResult(context.Html.GetEnumSelectList(EnumType ?? context.Metadata.ElementType ?? context.ModelType)));
 }
 protected abstract Task <IEnumerable <SelectListItem> > GetSelectListItemsAsync(SelectListContext context);
Exemple #4
0
        protected override async Task <IEnumerable <SelectListItem> > GetSelectListItemsAsync(SelectListContext context)
        {
            //Get DbContext from DI container
            DbContext db = (DbContext)context.HttpContext.RequestServices.GetService(DbContextType);

            if (db == null)
            {
                throw new Exception("Database not found");
            }

            //Get DbSet as IQueryable
            IQueryable query = (IQueryable)_dbContextSetMethod.MakeGenericMethod(ModelType).Invoke(db, null);

            if (!string.IsNullOrEmpty(RawSql))
            {
                query = (IQueryable)RelationalQueryableExtensions.FromSql((dynamic)query, RawSql, RawSqlParameters);
            }

            if (!EnableChangeTracking)
            {
                query = (IQueryable)EntityFrameworkQueryableExtensions.AsNoTracking((dynamic)query);
            }

            if (context.SelectedOnly)
            {
                //Select by Id
                var whereClause = DbContextHelper.SearchForEntityByValues(ModelType, DataValueField, context.CurrentValues);
                query = (IQueryable)_dbContextWhereClauseMethod.MakeGenericMethod(ModelType).Invoke(null, new object[] { query, whereClause });
            }
            else
            {
                if (context.ModelExplorer.Metadata is DefaultModelMetadata defaultModelMetadata)
                {
                    //Loop over where clauses

                    IEnumerable <SelectListDbWhereEqualsAttribute> whereClauseAttributes = null;
                    if (defaultModelMetadata.MetadataKind == ModelMetadataKind.Property)
                    {
                        whereClauseAttributes = defaultModelMetadata.Attributes.PropertyAttributes.OfType <SelectListDbWhereEqualsAttribute>().Where(a => a.SelectListId == this.SelectListId);
                    }
                    else if (defaultModelMetadata.MetadataKind == ModelMetadataKind.Type)
                    {
                        whereClauseAttributes = defaultModelMetadata.Attributes.TypeAttributes.OfType <SelectListDbWhereEqualsAttribute>().Where(a => a.SelectListId == this.SelectListId);
                    }

                    if (whereClauseAttributes != null)
                    {
                        foreach (var where in whereClauseAttributes)
                        {
                            var whereClause = LamdaHelper.SearchForEntityByProperty(ModelType, where.PropertyName, where.Values);
                            query = (IQueryable)_dbContextWhereClauseMethod.MakeGenericMethod(ModelType).Invoke(null, new object[] { query, whereClause });
                        }
                    }
                }
            }

            //Order By
            if (!string.IsNullOrWhiteSpace(OrderByProperty))
            {
                if (OrderByType == "asc")
                {
                    query = (IQueryable)_dbContextOrderByMethod.MakeGenericMethod(ModelType).Invoke(null, new object[] { query, OrderByProperty, true });
                }
                else
                {
                    query = (IQueryable)_dbContextOrderByMethod.MakeGenericMethod(ModelType).Invoke(null, new object[] { query, OrderByProperty, false });
                }
            }

            //Skip
            query = (IQueryable)Queryable.Skip((dynamic)query, Skip);

            //Take
            query = (IQueryable)Queryable.Take((dynamic)query, Take);

            //Get Results
            IEnumerable results = (IEnumerable)(await EntityFrameworkQueryableExtensions.ToListAsync((dynamic)query, CancellationToken.None));

            return(new ModelMultiSelectList(context.Html, results, DataValueField, DataTextFieldExpression));
        }
        protected async override Task <IEnumerable <SelectListItem> > GetSelectListItemsAsync(SelectListContext context)
        {
            var dataValueField = nameof(FileInfo.FullName);

            if (!Path.EndsWith(@"\"))
            {
                Path = Path + @"\";
            }

            var hostingEnvironment = context.HttpContext.RequestServices.GetRequiredService <IHostingEnvironment>();
            var mappedWwwPath      = hostingEnvironment.MapWwwPath(Path);
            var mappedContentPath  = hostingEnvironment.MapContentPath(Path);

            var searchPath = Path;
            var root       = "";
            var webFolder  = false;

            if (mappedWwwPath != mappedContentPath)
            {
                searchPath = mappedContentPath;
                root       = hostingEnvironment.ContentRootPath + @"\";
                if (Directory.Exists(mappedWwwPath))
                {
                    webFolder  = true;
                    searchPath = mappedWwwPath;
                    root       = hostingEnvironment.WebRootPath + @"\";
                }
            }

            var repository = _fileSystemGenericRepositoryFactory.CreateFileRepositoryReadOnly(default(CancellationToken), searchPath, IncludeSubDirectories, SearchPattern);
            var data       = await repository.GetAllAsync(LamdaHelper.GetOrderByFunc <FileInfo>(OrderByProperty, OrderByType), null, null);

            var results = new List <SelectListItem>();

            foreach (var item in data)
            {
                IHtmlHelper html = context.CreateHtmlHelper((dynamic)item);

                results.Add(new ModelSelectListItem()
                {
                    Model = item,
                    Html  = html,
                    Text  = RemoveSearchPathFromText ? context.Eval(html, item, DataTextFieldExpression).Replace(searchPath, "") : context.Eval(html, item, DataTextFieldExpression),
                    Value = RootRelativeValue ? webFolder ? context.Eval(html, item, dataValueField).Replace(root, "").Replace(@"\", @"/") : context.Eval(html, item, dataValueField).Replace(root, "") : context.Eval(html, item, dataValueField),
                });
            }

            return(results);
        }
        protected override Task <IEnumerable <SelectListItem> > GetSelectListItemsAsync(SelectListContext context)
        {
            var results = new List <SelectListItem>();

            for (int i = 0; i < _text.Length; i++)
            {
                IHtmlHelper html = context.CreateHtmlHelper((dynamic)_values[i]);

                results.Add(new ModelSelectListItem()
                {
                    Model = _values[i],
                    Html  = html,
                    Text  = _text[i].ToString(),
                    Value = _values[i].ToString()
                });
            }

            return(Task.FromResult(results.AsEnumerable()));
        }
 protected override Task <IEnumerable <SelectListItem> > GetSelectListItemsAsync(SelectListContext context)
 {
     if (context.Model != null)
     {
         return(Task.FromResult(new ModelMultiSelectList(context.Html, (IEnumerable)context.Model, DataValueField, DataTextFieldExpression, (IEnumerable)context.Model).AsEnumerable <SelectListItem>()));
     }
     else
     {
         return(Task.FromResult(Enumerable.Empty <SelectListItem>()));
     }
 }
        protected override Task <IEnumerable <SelectListItem> > GetSelectListItemsAsync(SelectListContext context)
        {
            Dictionary <string, RegionInfo> countryList = new Dictionary <string, RegionInfo>();

            CultureInfo[] cInfoList = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
            foreach (CultureInfo CInfo in cInfoList)
            {
                RegionInfo R = new RegionInfo(CInfo.LCID);
                if (!(countryList.ContainsKey(R.EnglishName)))
                {
                    countryList.Add(R.EnglishName, R);
                }
            }

            return(Task.FromResult(new ModelSelectList(context.Html, countryList.Keys.OrderBy(k => k).Select(k => countryList[k]), DataValueFieldExpression, DataTextFieldExpression).AsEnumerable <SelectListItem>()));
        }