Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc mvc操作方法中泛型类型参数的.net核心自定义模型绑定_Asp.net Mvc_Asp.net Core_Custom Model Binder_Actionmethod_Generic Type Argument - Fatal编程技术网

Asp.net mvc mvc操作方法中泛型类型参数的.net核心自定义模型绑定

Asp.net mvc mvc操作方法中泛型类型参数的.net核心自定义模型绑定,asp.net-mvc,asp.net-core,custom-model-binder,actionmethod,generic-type-argument,Asp.net Mvc,Asp.net Core,Custom Model Binder,Actionmethod,Generic Type Argument,我正在构建一个简单的搜索、排序、页面功能。我已经附上下面的代码。 以下是用例: 我的目标是通过每个请求传递“当前过滤器”,以持久化它们,特别是在排序和分页时 我想使用一个保存当前过滤器的泛型类型参数,而不是用很多(如果不是太多的话)参数来污染我的动作方法 我需要一个自定义的模型粘合剂,可以实现这一点 有人可以发布一个示例实现吗 PS:我也在探索替代方案,而不是来回传递复杂的对象。但我需要将此路径作为最后手段,并且我找不到自定义模型绑定泛型类型参数的好例子。任何指向此类示例的指针也会有所帮助。谢谢

我正在构建一个简单的搜索、排序、页面功能。我已经附上下面的代码。 以下是用例:

  • 我的目标是通过每个请求传递“当前过滤器”,以持久化它们,特别是在排序和分页时

  • 我想使用一个保存当前过滤器的泛型类型参数,而不是用很多(如果不是太多的话)参数来污染我的动作方法

  • 我需要一个自定义的模型粘合剂,可以实现这一点

  • 有人可以发布一个示例实现吗

    PS:我也在探索替代方案,而不是来回传递复杂的对象。但我需要将此路径作为最后手段,并且我找不到自定义模型绑定泛型类型参数的好例子。任何指向此类示例的指针也会有所帮助。谢谢

    public async Task<IActionResult> Index(SearchSortPage<ProductSearchParamsVm> currentFilters, string sortField, int? page)
    {
        var currentSort = currentFilters.Sort;
        // pass the current sort and sortField to determine the new sort & direction
        currentFilters.Sort = SortUtility.DetermineSortAndDirection(sortField, currentSort);
        currentFilters.Page = page ?? 1;
    
        ViewData["CurrentFilters"] = currentFilters;
    
        var bm = await ProductsProcessor.GetPaginatedAsync(currentFilters);
    
        var vm = AutoMapper.Map<PaginatedResult<ProductBm>, PaginatedResult<ProductVm>>(bm);
    
        return View(vm);
    }
    
    public class SearchSortPage<T> where T : class
    {
        public T Search { get; set; }
        public Sort Sort { get; set; }
        public Nullable<int> Page { get; set; }
    }
    
    public class Sort
    {
        public string Field { get; set; }
        public string Direction { get; set; }
    }
    
    public class ProductSearchParamsVm
    {
        public string ProductTitle { get; set; }
        public string ProductCategory { get; set; }
        public Nullable<DateTime> DateSent { get; set; }
    }
    
    公共异步任务索引(SearchSortPage currentFilters,string sortField,int?页)
    {
    var currentSort=currentFilters.Sort;
    //传递当前排序和排序字段以确定新的排序和方向
    currentFilters.Sort=排序性。确定排序和方向(sortField,currentSort);
    currentFilters.Page=第1页;
    ViewData[“CurrentFilters”]=CurrentFilters;
    var bm=等待产品处理器.GetPaginatedAsync(currentFilters);
    var vm=自动映射(bm);
    返回视图(vm);
    }
    公共类搜索SortPage,其中T:class
    {
    公共T搜索{get;set;}
    公共排序{get;set;}
    公共可空页{get;set;}
    }
    公共类排序
    {
    公共字符串字段{get;set;}
    公共字符串方向{get;set;}
    }
    公共类ProductSearchParamsVm
    {
    公共字符串ProductTitle{get;set;}
    公共字符串ProductCategory{get;set;}
    公共可为空的DateSent{get;set;}
    }
    
    首先创建应该实现接口IModelBinder的模型绑定器

    SearchSortPageModelBinder.cs

    public class SearchSortPageModelBinder<T> : IModelBinder
    {
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext == null)
            {
                throw new ArgumentNullException(nameof(bindingContext));
            }   
    
            SearchSortPage<T> ssp = new SearchSortPage<T>();
    
            //TODO: Setup the SearchSortPage<T> model 
    
            bindingContext.Result = ModelBindingResult.Success(ssp);
    
            return TaskCache.CompletedTask;
        }
    }
    
    public class SearchSortPageModelBinderProvider : IModelBinderProvider
    {
        public IModelBinder GetBinder(ModelBinderProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
    
            if (context.Metadata.ModelType.GetTypeInfo().IsGenericType && 
                context.Metadata.ModelType.GetGenericTypeDefinition() == typeof(SearchSortPage<>))
            {
                Type[] types = context.Metadata.ModelType.GetGenericArguments();
                Type o = typeof(SearchSortPageModelBinder<>).MakeGenericType(types);
    
                return (IModelBinder)Activator.CreateInstance(o);
            }
    
            return null;
        }
    }
    
    public void ConfigureServices(IServiceCollection services)
    {
            .
            .
    
            services.AddMvc(options=>
            {
                options.ModelBinderProviders.Insert(0, new SearchSortPageModelBinderProvider());
            });
            .
            .
    }