Open kergee opened 7 years ago
I modified like this:
''' using BookstoreWebApp.ViewModels; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using System; using System.Collections.Generic;
namespace BookstoreWebApp.Filters { [AttributeUsage(AttributeTargets.Method)] public class GenerateResultListFilterAttribute : ResultFilterAttribute, IResultFilter { private readonly Type _sourceType;
private readonly Type _desinationType;
public GenerateResultListFilterAttribute(Type sourceType, Type destionationType)
{
_sourceType = sourceType;
_desinationType = destionationType;
}
public override void OnResultExecuting(ResultExecutingContext context)
{
Controller controller = (Controller)context.Controller;
object model = controller.ViewData.Model;
Type resultListGenericType = typeof(ResultListViewModel<>).MakeGenericType(new Type[] { _desinationType });
Type sourceGenericType = typeof(List<>).MakeGenericType(new Type[] { _sourceType });
Type destinationGenericType = typeof(List<>).MakeGenericType(new Type[] { _desinationType });
// It equals to the code AutoMapper.Mapper.CreateMap(_sourceType, _destinationType);
AutoMapper.Mapper.Initialize(config => config.CreateMap(_sourceType, _desinationType));
object viewModel = AutoMapper.Mapper.Map(model, sourceGenericType, destinationGenericType);
// from QueryOptions
var sortField = controller.ViewData.ContainsKey("SortField") ? controller.ViewData["SortField"] : "default";
// from QueryOptions
var sortOrder = controller.ViewData.ContainsKey("SortOrder") ? controller.ViewData["SortOrder"] : "desc";
// from QueryOptions
var currentPage = controller.ViewData.ContainsKey("CurrentPage") ? controller.ViewData["CurrentPage"] : 1;
var resultList = Activator.CreateInstance(resultListGenericType, viewModel, sortField, sortOrder, currentPage);
controller.ViewData.Model = resultList;
}
public override void OnResultExecuted(ResultExecutedContext context)
{
base.OnResultExecuted(context);
}
}
}
'''
can't be passed, how correct it?
namespace BootstrapIntroduction.Filters { [AttributeUsage(AttributeTargets.Method)] public class GenerateResultListFilterAttribute : FilterAttribute, IResultFilter { private readonly Type _sourceType; private readonly Type _destinationType;
}