I have this code:
public ActionResult Details(string id, string detailsDate)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var tblPersonnel = new tblPersonnel();
using (var _context = new ScalehouseModel()) // for disposing
{
tblPersonnel = _context.tblPersonnels.Find(id);
}
if (tblPersonnel == null)
{
return HttpNotFound();
}
Mapper.Initialize(config => config.CreateMap<tblPersonnel, PersonnelDetailsVm>());
PersonnelDetailsVm person = Mapper.Map<tblPersonnel, PersonnelDetailsVm>(tblPersonnel);
.... // and more but the error happens on the line above.
}
After an ajax success, I am redirecting to the Details page, which the action you can see above. I am getting run time errors like so:
I have researched this and disposed my EF, but still not working and getting same error.
What do I need to do to resolve this?
In order for auto mapper to work, best way is to add automapper configuration in pipeline. Here is how you can do
Step 1: Create a static class Mapping profile, and add all your mappings in this class
public static class MappingProfile
{
public static MapperConfiguration InitializeAutoMapper()
{
MapperConfiguration config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<tblPersonnel, PersonnelDetailsVm>();
});
return config;
}
}
Step 2: Inorder to configure, register automapper configuration in Application_Start() method in Global.asax.cs
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
//Add AutoMapper configuration here.
MappingProfile.InitializeAutoMapper();
}
Give it a try.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.