In my MVC webapplication I got the following automapper code:
The configure:
public static void Configure()
{
Mapper.CreateMap<string, List<Ruimte>>().ConvertUsing<StringCombinatieRuimtesConverter>();
}
The custom converter:
public class StringCombinatieRuimtesConverter : ITypeConverter<string, List<Ruimte>>
{
#region Implementation of ITypeConverter<in string,out List<Ruimte>>
public List<Ruimte> Convert(ResolutionContext context)
{
if (context.SourceValue == null)
{
return new List<Ruimte>();
}
return context.SourceValue.ToString().Split(',').Select(r => new Ruimte { Id = int.Parse(r) }).ToList();
}
#endregion
}
And I call it with the following code:
var ruimteList = new List<Ruimte>();
// Update the CombinatieVan
Mapper.Map(command.CombinatieRuimteVan, ruimteList);
If I debug the command.CombinatieRuimteVan is: 2,3,4,6. This is fine.
Now if I debug the converter it does return 4 objects of the class “Ruimte” with as Id the numbers stated before. But after the Mapper.Map method is done the “ruimteList” is still an empty list.
Does anyone know what is going wrong here? The automapper works fine on another spot in my application where I automap a whole entity containing the “CombinatieRuimteVan” set. But when I sololy convert the string to the list it doesn’t seem to work.
In your converter you have add the created object instances to the destination List<>. The List<> should be accessible through context.DestinationValue. If you don’t take the context.DestinationValue in account, your mapped objects are only returned as the result of the Map() method. Your converter should look somewhat like this: