Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8370515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:55:50+00:00 2026-06-09T13:55:50+00:00

I’ve been fiddling about and trying multiple things, but I’m going wrong somewhere. I

  • 0

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.

Domain:

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
}

ViewModel:

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

Controller

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.

    [HttpPost]
    public ActionResult Create(CreateBrandViewModel createBrandViewModel)
    {
        if(ModelState.IsValid)
        {

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

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

        else
        {
            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)
            .Not.Nullable()
            .Length(50);

        Map(x => x.IsActive)
            .Not.Nullable();

        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);
            Session.SaveOrUpdate(updatedBrand);
            return RedirectToAction("Index");
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:55:52+00:00Added an answer on June 9, 2026 at 1:55 pm

    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
    Session.SaveOrUpdate(domainModel);
    

    let me know if that cracks the egg… or just egg on yer face again 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.