2

Everything was fine, until I added DataTime property to my Model and viewModel. All data is correct, but error occurs anyway. I think the error appears because of I added PublicationDate. As I understand, Automapper2.0 should work fine with dates,so I dunno why does it happen

Error occurs here:

Publication viewModelPublication = new Publication(Mapper.Map<PublicationViewModel, Publication>(viewModel));

My AutoMapperConfig:

 public class AutoMapperConfig
{
    public static void Initialize()
    {
        Mapper.Initialize(cfg =>
        {

            cfg.CreateMap<Author, AuthorViewModel>()
           .ForMember(a => a.AuthorId, opt => opt.MapFrom(a => a.AuthorId))
           .ForMember(a => a.AuthorName, opt => opt.MapFrom(a => a.AuthorName));

            cfg.CreateMap<AuthorViewModel, Author>()
          .ForMember(a => a.AuthorId, opt => opt.MapFrom(a => a.AuthorId))
          .ForMember(a => a.AuthorName, opt => opt.MapFrom(a => a.AuthorName));


            cfg.CreateMap<Publication, PublicationViewModel>()
         .ForMember(p => p.PublicationId, opt => opt.MapFrom(p => p.PublicationId))
         .ForMember(p => p.PublicationName, opt => opt.MapFrom(p => p.PublicationName))
         .ForMember(p => p.Pages, opt => opt.MapFrom(p => p.Pages))
         .ForMember(p => p.Publisher, opt => opt.MapFrom(p => p.Publisher))
         .ForMember(p => p.PublicationDate, opt => opt.MapFrom(p => p.PublicationDate))
         .ForMember(p => p.Subscription, opt => opt.MapFrom(p => p.Subscription))
         .ForMember(p => p.Authors, opt => opt.MapFrom(p => p.Authors.ToList()));

            cfg.CreateMap<PublicationViewModel, Publication>()
            .ForMember(p => p.PublicationId, opt => opt.MapFrom(p => p.PublicationId))
            .ForMember(p => p.PublicationName, opt => opt.MapFrom(p => p.PublicationName))
            .ForMember(p => p.Pages, opt => opt.MapFrom(p => p.Pages))
            .ForMember(p => p.Publisher, opt => opt.MapFrom(p => p.Publisher))
            .ForMember(p => p.PublicationDate, opt => opt.MapFrom(p => p.PublicationDate))
            .ForMember(p => p.Subscription, opt => opt.MapFrom(p => p.Subscription))
            .ForMember(p => p.Authors, opt => opt.MapFrom(p => p.Authors));

        });
    }
}

My models:

 public class PublicationViewModel
{
    public PublicationViewModel()
    {
        Authors = new List<AuthorViewModel>();
    }
    public int PublicationId { get; set; }

    [Display(Name = "Publication Name")]
    public string PublicationName { get; set; }

    public int Pages { get; set; }

    public string Publisher { get; set; }

    [Display(Name = "Publication Date")]
    public DateTime PublicationDate { get; set; }

    public SubscriptionViewModel Subscription { get; set; }

    [UIHint("AuthorsEditor")]
    public virtual List<AuthorViewModel> Authors { get; set; }
}
 public class Publication
{
    public Publication()
    {

    }
    public Publication(Publication publication)
    {
        PublicationId = publication.PublicationId;
        PublicationName = publication.PublicationName;
        Pages = publication.Pages;
        Publisher = publication.Publisher;
        Subscription = publication.Subscription;
        PublicationDate = publication.PublicationDate;
        Authors = publication.Authors;
    }

    public int PublicationId { get; set; }

    public string PublicationName { get; set; }

    public int Pages { get; set; }

    public string Publisher { get; set; }

    public DateTime PublicationDate { get; set; }

    public Subscription Subscription { get; set; }

    public virtual ICollection<Author> Authors { get; set; }
}
1
  • There's no javascript here. I've removed your javascript tag. Commented Mar 14, 2018 at 7:28

1 Answer 1

1

I've found a problem. I forgot to send id from my subscriptionViewModel(Subscription is an enum type).

.ForMember(p => p.Subscription, opt => opt.MapFrom(p => p.Subscription.SubscriptionId))//here
Sign up to request clarification or add additional context in comments.

Comments

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.