2

I have an Attendee Class and An AttendeeViewModel

The datetime field on the Attendee Model gets set to the default .NET Datetime when i map it from AttendeeViewModel instead of the value that is already existing in the Attendee Model

Here's my AttendeeViewModel

public class AttendeeViewModel
{

    public int Id { get; set; }
    [Required]
    [EmailAddress]
    public string Email { get; set; }
    [Required]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }
    [Required]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }
    public int FEventId { get; set; }

    public string DisplayName
    {
        get { return string.Format("{0}, {1}", FirstName, LastName); }
    }
}

Here's my Base AttendeeModel

public class Attendee
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public  int Id { get; set; }
    [Required]
    public  string Email { get; set; }
    [Required]
    public  string FirstName { get; set; }
    [Required]
    public  string LastName { get; set; }
    public DateTime CreatedAt { get; set; }

    public bool IsActive { get; set; }


    public int FEventId { get; set; }

    public virtual ApplicationUser CreatedBy { get; set; }

    public virtual FEvent FEvent { get; set; }

    public ICollection<ProjectPledge> ProjectPledges { get; set; } 

}

Here's My mapping configuration

public static void Configure()
    {

       Mapper.CreateMap<AttendeeViewModel, Attendee>().ForMember(dest=>dest.CreatedAt , opt=>opt.Ignore());

    } 

And heres's the Controller Action

[HttpPost]
    [ValidateAntiForgeryToken]
    public virtual ActionResult Edit(AttendeeViewModel attendee)
    {
        if (!_attendeeService.CanAddAttendee(attendee.Email, attendee.FilanthropyEventId))
        {
            AddEmailModelError();
        }

        if (ModelState.IsValid)
        {
            var mappedAttendee = _attendeeService.GetById(attendee.Id);

            mappedAttendee = Mapper.Map<AttendeeViewModel, Attendee>(attendee);
            _attendeeService.AddOrUpdate(mappedAttendee);
            return RedirectToAction(MVC.Attendee.Index(mappedAttendee.FilanthropyEventId));
        }
        return View(attendee);
    }

if I set the configuration to be this insetad of opt.Ignore()

Mapper.CreateMap<AttendeeViewModel, Attendee>().ForMember(dest=>dest.CreatedAt , opt=>opt.UseDestinationValue());

The Mapping fails giving this exception

Missing type map configuration or unsupported mapping.

Mapping types:
AttendeeViewModel -> DateTime
MyProject.Web.ViewModels.AttendeeViewModel -> System.DateTime

Destination path:
Attendee.CreatedAt.CreatedAt

Source value:
MyProject.Web.ViewModels.AttendeeViewModel

Any ideas on how i can resolve this?

2
  • 1
    Here is a tip, at the end of Configure() add a Mapper.AssertConfigurationIsValid(), this will cause the exception to be thrown in your Configure() instead of waiting till your first call of Map to have it thrown. (No idea how to solve your problem, sorry :( ) Commented Jan 21, 2014 at 20:13
  • Yeah, I've been using that, but thanks anyway Commented Jan 22, 2014 at 0:04

2 Answers 2

2

If you want to map onto an existing object you need to use the overload that takes the existing destination:

Mapper.Map<Source, Destination>(source, destination);

that should do the trick.

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried removing the ".ForMember" section and just let AutoMapper ignore it? In order to help you any more it would be helpful to see the two models for comparison.

Update: after lookind at your models I would suggest the following should solve the issue you are having...

Mapper.CreateMap <attendeeviewmodel, attendee>.ForMember (x => x.CreatedAt, opt => opt.MapFrom (src => datetime.utcnow));

4 Comments

Still sets the Destination DataTime to a default. Will post the models up later tonight
Did you sort your issue?
Sorry, i should have been a bit more explicit in my question. I want the mapped value to be the original value from the attendee object, not the current time
Have you tried adding ".IgnoreAllUnmapped()" at the end?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.