Note that there are some explanatory texts on larger screens.

plurals
  1. POStruggling to get AutoMapper to map my ViewModel to my Domain, What could I be doing wrong?
    primarykey
    data
    text
    <p>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.</p> <p>Domain:</p> <pre><code>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&lt;Product&gt; Products { get; set; } // Nullable } </code></pre> <p>ViewModel:</p> <pre><code>public class CreateBrandViewModel { public string Name { get; set; } public bool IsActive { get; set; } } </code></pre> <p>Controller </p> <p>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. </p> <pre><code> [HttpPost] public ActionResult Create(CreateBrandViewModel createBrandViewModel) { if(ModelState.IsValid) { Mapper.CreateMap&lt;Brand, CreateBrandViewModel&gt;(); //.ForMember( // dest =&gt; dest.Name, // opt =&gt; opt.MapFrom(src =&gt; src.Name) //) //.ForMember( // dest =&gt; dest.IsActive, // opt =&gt; opt.MapFrom(src =&gt; src.IsActive) //); Mapper.Map&lt;Brand, CreateBrandViewModel&gt;(createBrandViewModel) Session.SaveOrUpdate(createBrandViewModel); return RedirectToAction("Index"); } else { return View(createBrandViewModel); } } </code></pre> <p>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.</p> <p>For reference, my fluent nhibernate mapping for Brand:</p> <pre><code>public class BrandMap : ClassMap&lt;Brand&gt; { public BrandMap() { Id(x =&gt; x.Id); Map(x =&gt; x.Name) .Not.Nullable() .Length(50); Map(x =&gt; x.IsActive) .Not.Nullable(); References(x =&gt; x.DefaultProduct); HasMany(x =&gt; x.Products); } } </code></pre> <p><strong>Edit 1</strong></p> <p>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:</p> <pre><code> var brand = new Brand(); var updatedBrand = Mapper.Map&lt;Brand, CreateBrandViewModel&gt;(brand, createBrandViewModel); Session.SaveOrUpdate(updatedBrand); return RedirectToAction("Index"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload