1

私は単純な検索、並べ替え、ページ機能を構築しています。私は以下のコードを添付しています。 は以下の使用事例です:mvcアクションメソッドのジェネリック型パラメータのドットネットコアカスタムモデルバインド

  1. 私の目標は、ソートやページング特にながら、それらを永続化するために各要求を経由して「現在のフィルタ」を通過させることです。

  2. 私のアクションメソッドを多くの(あまりにも多くはないが)パラメータで汚染するのではなく、現在のフィルタを保持するジェネリック型のパラメータを使用することを考えています。

  3. これを実現できるカスタムモデルバインダーが必要です。

例の実装を投稿できますか?

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; } 
} 

答えて

2

まずインタフェースを実装する必要があるモデルバインダーを作成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; 
    } 
} 

そして、インタフェースを実装する必要があるモデルバインダープロバイダーを作成IModelBinderProvider

SearchSortPageModelBinderProvider.cs

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; 
    } 
} 

そして最後は、モデルバインダープロバイダーを登録している、それはあなたのStartup.cs

public void ConfigureServices(IServiceCollection services) 
{ 
     . 
     . 

     services.AddMvc(options=> 
     { 
      options.ModelBinderProviders.Insert(0, new SearchSortPageModelBinderProvider()); 
     }); 
     . 
     . 
} 
+0

おかげで行う必要があります。私はこれをチェックします。 – blogs4t

関連する問題