I am trying to use AutoMapper 6.3 to allow me to auto map my models into view-models.
First I registered my AutoMapper instance to my IUnitContainer
like so
var mapper = new MapperConfiguration(cfg =>
{
cfg.CreateMap<TaskViewModel, Task>();
});
container.RegisterInstance<IMapper>(mapper.CreateMapper());
container.RegisterType<IUserPassport, UserPassport>(); // This has nothing to do with AutoMapper but is needed to convert date time.
Now, in my controller, I want to pull a model from the database, then I want to map/cast it to my view-model.
I tried to do the following
var task = UnitOfWork.Tasks.Find(x => x.Id == 123)
.ProjectTo<TaskViewModel>()
.FirstOrDefault();
However, my view model need's to know current user's time zone in order to convert time from UTC to local time zone. Somehow, I need to be able to pass Passport.User.TimeZone
to the ViewModel to be able to process datetime converson?
Here is how I am trying to project the viewModel
public class TaskController : Controller
{
private IUserPassport Passport;
public TaskController(IUserPassport passport)
{
Passport = passport;
}
public ActionResult Show(int? id)
{
if (!id.HasValue)
{
return HttpNotFound();
}
// Some how Passport.User.TimeZone needs to be passed to the
// TaskViewModel to the time stamps are converted.
var task = UnitOfWork.Tasks.Find(x => x.Id == id.Value)
.ProjectTo<TaskViewModel>()
.FirstOrDefault();
return View(task);
}
}