Exemple #1
0
        public void Speaker_should_map_to_SpeakerForm_with_valid_data()
        {
            var speaker = new Speaker
            {
                Email = "[email protected]",
                Languages = new [] {"JavaScript", "C#"},
                Name = "Brandon Satrom",
                SpeakerId = 2343,
                State = "TX"
            };
            var session = new Session
            {
                Speaker = speaker,
                Date = DateTime.Now,
                Id = 123,
                Location = "Here",
                Name = "Automapper for Noobs",
                Ratings = new List<int> { 3, 5, 2, 1, 3 }
            };
            var sessions = new List<Session> {session, session};

            speaker.Sessions = sessions;

            Mapper.CreateMap<Session, string>().ConvertUsing(s => s.Name);
            Mapper.CreateMap<Speaker, SpeakerForm>();

            SpeakerForm speakerForm = Mapper.Map<Speaker, SpeakerForm>(speaker);

            Assert.IsNotNull(speakerForm);
            Assert.AreEqual("Brandon Satrom", speakerForm.Name);
            Assert.AreEqual(2, speakerForm.Sessions.Count());
        }
 public ActionResult Create(Speaker speaker)
 {
     if (ModelState.IsValid) {
         speakerRepository.InsertOrUpdate(speaker);
         speakerRepository.Save();
         return Json("Speaker information saved!");
     } else {
         return View();
     }
 }
 public void InsertOrUpdate(Speaker speaker)
 {
     if (speaker.SpeakerId == default(int)) {
         // New entity
         context.Speakers.Add(speaker);
     } else {
         // Existing entity
         context.Speakers.Attach(speaker);
         context.Entry(speaker).State = EntityState.Modified;
     }
 }
 public void InsertOrUpdate(Speaker speaker)
 {
     throw new NotImplementedException();
 }
 public ActionResult Edit(Speaker speaker)
 {
     if (ModelState.IsValid) {
         speakerRepository.InsertOrUpdate(speaker);
         speakerRepository.Save();
         return RedirectToAction("Index");
     } else {
         return View();
     }
 }