Home > DeveloperSection > Forums > Correct way to use AutoMapper in ASP.Net MVC
Tanuj Kumar

Total Post:134

Points:940
Posted on    May-10-2013 2:21 AM

 C# C# 
Ratings:


 1 Reply(s)
 997  View(s)
Rate this:
Hi Expert!

I'm trying to tart using ViewModels - but I'm having trouble with this POST not validating - the values in the model are shown in the Watch part below the code:

ModelStats.IsValid = false

My ItemViewModel is:

  public class ItemViewModel
  {
    public int ItemId { get; set; }
    [Display(Name = "Item")]
    public string ItemName { get; set; }
    [Display(Name = "Description")]
    public string Description { get; set; }
    [Display(Name = "Price")]
    public double UnitPrice { get; set; }
    [Range(0.00, 100, ErrorMessage = "VAT must be a % between 0 and 100")]
    public decimal VAT { get; set; }
    [Required]
    public string UserName { get; set; }
   }
I'm sure it will be something simple - but I've just been looking at it so long, I can't figure out what I'm doing wrong. Can anyone please advise?

Thanks in advance!


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    May-10-2013 9:13 AM

Hi Tanuj!

In order to Use AutoMapper

First, Create a map, such as

 Mapper.CreateMap<Item, ItemViewModel>();

And then map

var itemModel = Mapper.Map<Item, ItemViewModel>(model);

Don't want to miss updates? Please click the below button!

Follow MindStick