Search code examples
c#asp.net-mvcautomapperpagedlist

How to use AutoMapper with PagedList?


I'd like to use AutoMapper in order to map my ViewModel to domain model class. Also I'm using PagedList NuGet Package. I'm using it this way:

[Authorize]
[AutoMap(typeof(ErrorsLog), typeof(ErrorsLogViewModel))]
public ActionResult Errors(string searchString, string currentFilter, int? page)
{
    if (searchString != null)
    {
        page = 1;
    }
    else
    {
        searchString = currentFilter;
    }

    var el = _er.GetErrorsLog();
    ViewBag.CurrentFilter = searchString;

    if (!String.IsNullOrEmpty(searchString))
    {
        el = el.Where(s => s.ErrorSource.Contains(searchString));
    }

    const int pageSize = 3;
    int pageNumber = (page ?? 1);
    return View("Errors", el.ToPagedList(pageNumber, pageSize));
}

Unfortunately I got error:

Missing type map configuration or unsupported mapping. Mapping types: ErrorsLog -> ErrorsLogViewModel DigitalHubOnlineStore.Models.ErrorsLog -> DigitalHubOnlineStore.ViewModels.ErrorsLogViewModel Destination path: ErrorsLogViewModel Source value: PagedList.PagedList`1[DigitalHubOnlineStore.Models.ErrorsLog]

How can I fix that?


Solution

  • Did you have register your mappings? By the error message, it seems that you didn't call the CreateMap method anywhere yet.
    Take a look at this.

    EDIT

    As mentioned here, you can create a static class for your mappings...

    public static class AutoMapperConfig
    {
       public static void Configure()
       {    
          Mapper.CreateMap<ErrorsLog, ErrorsLogViewModel>();
       }
    }
    

    and just call it in your Global.asax:

    AutoMapperConfig.Configure();