Take the 2-minute tour ×

I've been fiddling about and trying multiple things, but I'm going wrong somewhere. I tried to make my first attempt using AutoMapper as simple as possible. I'm trying to create a new Brand and save it to the database, using a CreateBrandViewModel. Some of this might look a bit fruity, but I was trying to get it to work in the simplest way possible.


public class Brand : EntityBase
    public virtual string Name { get; set; } //Not Nullable
    public virtual bool IsActive { get; set; } // Not Nullable
    public virtual Product DefaultProduct { get; set; } // Nullable
    public virtual IList<Product> Products { get; set; } // Nullable


public class CreateBrandViewModel
    public string Name { get; set; }
    public bool IsActive { get; set; }


this is where I've been playing about the most for a while, so it looks a bit strange now. The commented out code hasn't resolved my problem.

    public ActionResult Create(CreateBrandViewModel createBrandViewModel)

            Mapper.CreateMap<Brand, CreateBrandViewModel>();
                //    dest => dest.Name,
                //    opt => opt.MapFrom(src => src.Name)
                //    dest => dest.IsActive,
                //    opt => opt.MapFrom(src => src.IsActive)

            Mapper.Map<Brand, CreateBrandViewModel>(createBrandViewModel)
            return RedirectToAction("Index");

            return View(createBrandViewModel);

Just for the record, BrandController inherits from SessionController (Ayendes way), and transactions are managed through an ActionFilter. Though thats is a bit irrelevant I think. I've tried various different ways so I have different error messages - if you can take a look at whats happening and tell me how you might expect to use it that would be great.

For reference, my fluent nhibernate mapping for Brand:

public class BrandMap : ClassMap<Brand>
    public BrandMap()
        Id(x => x.Id);

        Map(x => x.Name)

        Map(x => x.IsActive)

        References(x => x.DefaultProduct);

        HasMany(x => x.Products);


Edit 1

I just tried the following code, but putting a breakpoint on Session.SaveOrUpdate(updatedModel) the fields are null and false, when they shouldn't be:

            var brand = new Brand();
            var updatedBrand = Mapper.Map<Brand, CreateBrandViewModel>(brand, createBrandViewModel);
            return RedirectToAction("Index");
share|improve this question
Thought I'd throw in a suggestion for you even though im late on this question, instead of calling CreateMap in your controller, you could create a profile and have that profile loaded in App_Start or Global.asax that way every time your controller is called, you are not causing over head of calling the same mapping code over and over, it only needs to be mapped once per application start. –  Patrick Magee Feb 20 '13 at 20:07
add comment

1 Answer

up vote 2 down vote accepted

You appear to be doing your mapping the wrong way around on your return trip from the post. The alternative syntax may help out here, try:

// setup the viewmodel -> domain model map 
// (this should ideally be done at initialisation time, rather than per request)
Mapper.CreateMap<CreateBrandViewModel, Brand>();
// create our new domain object
var domainModel = new Brand();
// map the domain type to the viewmodel
Mapper.Map(createBrandViewModel, domainModel);
// now saving the correct type to the db

let me know if that cracks the egg... or just egg on yer face again :-)

share|improve this answer
Ahhh yeah I see it now - wasn't exactly what you posted but the important part was getting the CreateMap the wrong way around. Thanks a bunch! Corrected line was: Mapper.Map<CreateBrandViewModel, Brand>(createBrandViewModel, domainModel); –  Kiada Aug 10 '12 at 11:30
craig -consider the egg well and truly cracked. have a great wknd, glad to have been a cog in the wheel... –  jim tollan Aug 10 '12 at 11:31
Will do, much appreciated :) –  Kiada Aug 10 '12 at 11:35
add comment

Your Answer


