2

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?

Daniel Oliveira
  • 1,101
  • 9
  • 26
andrey.shedko
  • 3,128
  • 10
  • 61
  • 121

1 Answers1

4

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();
Community
  • 1
  • 1
Daniel Oliveira
  • 1,101
  • 9
  • 26
  • Yes, I did not. Where I should call this method? In controller or Global.asax? – andrey.shedko Jun 13 '15 at 15:00
  • 3
    You know, it's better to include the essential parts of the answer here (not in a link) and provide the link for reference. Link-only answers can become invalid if the linked page changes. – ZygD Jun 13 '15 at 15:01
  • 1
    You are right @ZygD, sorry! Andrey, I've edited the answer with the basic configuration needs, hope that helps! – Daniel Oliveira Jun 13 '15 at 15:19