// HomeController.cs
[AutoMap(typeof(Test), typeof(TestViewModel))]
public ActionResult Index()
{
var tests = new List<Test>();
tests.Add(new Test());
return View(tests);
}
// Index.cshtml
@model IEnumerable<AutoMapperTest.Models.TestViewModel>
// AutoMapper Configuration
CreateMap<Test, TestViewModel>();
// Automapper configuration test is fine
[TestMethod]
public void Should_map_dtos()
{
AutoMapperConfiguration.Configure();
Mapper.AssertConfigurationIsValid();
}
AutoMapAttribute taken from here.
If the action return 'Test' it works. If it returns 'IEnumerable' it doesn't (view changed to expect TestViewModel and IEnumerable obviously). What am I doing wrong? From what I understand I don't need to explicitly map List types.
This probably has a simple answer but have spent the last two hours trying to get this to work so would much appreciate someone pointing me to the solution.
It's not clear to me from your description whether or not you changed the AutoMapper attribute to include the List or not:
[AutoMapper(typeof(List<Test>), typeof(List<TestViewModel>)]