开发者

Help understanding the basics of AutoMapper

So I have this two classes:

public class PhysicalTest
    {
        public int ID { get; set; }       
        public DateTime CreationDate { get; set; }
        public int Weight { get; set; }
        public int Height { get; set; }
        public int Systolic { get; set; }  
        public int Diastolic { get; set; } 
        public int Pulse { get; set; }
}

public class PhysicalTestFormViewModel
    {
        public int ID { get; set; }       
        public DateTime CreationDate { get; set; }
        [Required]
        public int Weigh开发者_运维技巧t { get; set; }
        [Required]
        public int Height { get; set; }
        public int Systolic { get; set; }  
        public int Diastolic { get; set; } 
        public int Pulse { get; set; }
}

This is my AutoMapper configuration

Mapper.CreateMap<PhysicalTestFormViewModel, PhysicalTest>();

When I do this it works just fine

 [HttpPost]
        public ActionResult Create(int ehrId, PhysicalTestFormViewModel physicaltestvm)
        {
            EHR ehr = ehrRepository.Find(ehrId);
            if (ehr.UserName != User.Identity.Name)
                return View("Invalid Owner");

            if (ModelState.IsValid)
            {
                PhysicalTest physicalTest= new PhysicalTest();
                Mapper.Map(physicaltestvm, physicalTest);
                physicalTest.PerformedBy = "Yo";
                physicalTest.CreationDate = DateTime.Now;
                ehr.PhysicalTests.Add(physicalTest);
                ehrRepository.Save();
                return RedirectToAction("Index");
            }
            else
            {
                return View(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.

public ActionResult Edit(int ehrId, int id)
        {
            EHR ehr = ehrRepository.Find(ehrId);
            if (ehr.UserName != User.Identity.Name)
                return View("Invalid Owner");
            var physicalTest = ehr.PhysicalTests.Where(test => test.ID == id).Single();
            PhysicalTestFormViewModel physicaltestvm = new PhysicalTestFormViewModel(); 
            Mapper.Map(physicalTest, physicaltestvm); 
            return View(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?


You have only defined a mapping from PhysicalTestFormViewModel to PhysicalTest:

Mapper.CreateMap<PhysicalTestFormViewModel, PhysicalTest>();

You also need the opposite one:

Mapper.CreateMap<PhysicalTest, PhysicalTestFormViewModel>();

See this related SO question and answers.


you may do dynamic mapping where you dont have to create any maps

public ActionResult (PhysicalTestFormViewModel ptvm)
{
  //other to wrote codes
EHR ehr = ehrRepository.Find(ehrId);

AutoMapper.Mapper.DynamicMap<PhysicalTestFormViewModel, PhysicalTest>(ptvm, ehr);

db.SaveChanges();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜