Login with  Log in with facebook
Hiring Manager? SIGN UP HERE
0

So I have this two classes:

publicclassPhysicalTest
{
publicint ID {get;set;}
publicDateTimeCreationDate{get;set;}
publicintWeight{get;set;}
publicintHeight{get;set;}
publicintSystolic{get;set;}
publicintDiastolic{get;set;}
publicintPulse{get;set;}
}

publicclassPhysicalTestFormViewModel
{
publicint ID {get;set;}
publicDateTimeCreationDate{get;set;}
[Required]
publicintWeight{get;set;}
[Required]
publicintHeight{get;set;}
publicintSystolic{get;set;}
publicintDiastolic{get;set;}
publicintPulse{get;set;}
}

This is my AutoMapper configuration

Mapper.CreateMap<PhysicalTestFormViewModel,PhysicalTest>();

When I do this it works just fine

[HttpPost]
publicActionResultCreate(int ehrId,PhysicalTestFormViewModel physicaltestvm)
{
EHR ehr
= ehrRepository.Find(ehrId);
if(ehr.UserName!=User.Identity.Name)
returnView("Invalid Owner");

if(ModelState.IsValid)
{
PhysicalTest physicalTest=newPhysicalTest();
Mapper.Map(physicaltestvm, physicalTest);
physicalTest
.PerformedBy="Yo";
physicalTest
.CreationDate=DateTime.Now;
ehr
.PhysicalTests.Add(physicalTest);
ehrRepository
.Save();
returnRedirectToAction("Index");
}
else
{
returnView(physicaltestvm);
}
}

But when I do this I get an error

Trying to map Summumnet.PhysicalTest to Summumnet.ViewModels.PhysicalTestFormViewModel. Missing type map configuration or unsupported mapping. Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.

publicActionResultEdit(int ehrId,int id)
{
EHR ehr
= ehrRepository.Find(ehrId);
if(ehr.UserName!=User.Identity.Name)
returnView("Invalid Owner");
var physicalTest = ehr.PhysicalTests.Where(test => test.ID == id).Single();
PhysicalTestFormViewModel physicaltestvm =newPhysicalTestFormViewModel();
Mapper.Map(physicalTest, physicaltestvm);
returnView(physicaltestvm);
}

In the scenario where the error is thrown I simply want to construct an ViewModel to display an Edit form.... what is the standard way of doing this?

Maneshwar Meel
07/08/2011 at 03:54
2 Answers
1
0

You have only defined a mapping from PhysicalTestFormViewModel to PhysicalTest:

Mapper.CreateMap<PhysicalTestFormViewModel,PhysicalTest>();

You also need the opposite one:

Mapper.CreateMap<PhysicalTest,PhysicalTestFormViewModel>();
Dewasish Mitruka
07/08/2011 at 06:30

2
0

From my experience i also had a couple of problems but i found that AutoMapper.DynMap() works in cases where AutoMapper.Map() fails.

Carlos Souto
11/05/2011 at 07:50

If you want to post any answer to this forum then you need to log in.
Schedule a Demo

Schedule a Demo with us

Name *
Email *
Phone *
Company *
Details