AutoMapper setting destination object to null

2019-07-25 02:06发布

问题:

I have been following this article: https://medium.com/ps-its-huuti/how-to-get-started-with-automapper-and-asp-net-core-2-ecac60ef523f

But when I get to the actual mapping part my destination is null. Can anyone tell me what I'm doing wrong?

Startup.cs

public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddDbContext<AppDbContext>(
            options => options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));

        services.AddScoped<ICreditCardApplicationRepository,
                           EntityFrameworkCreditCardApplicationRepository>();

        services.AddAutoMapper();

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

CreditCardApplicationProfile

public class CreditCardApplicationProfile : Profile
{
    public CreditCardApplicationProfile()
    {
        CreateMap<NewCreditCardApplicationDetails, CreditCardApplication>();
    }   
}

Full ApplyController.cs, but I am interested in the HttpPost Index method.

public class ApplyController : Controller
{
    private readonly ICreditCardApplicationRepository _applicationRepository;
    private readonly IMapper _mapper;


    public ApplyController(
        ICreditCardApplicationRepository applicationRepository,
        IMapper mapper
    )
    {
        _applicationRepository = applicationRepository;
        _mapper = mapper;
    }
    public IActionResult Index()
    {
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Index(NewCreditCardApplicationDetails applicationDetails)
    {
        if (!ModelState.IsValid)
        {
            return View(applicationDetails);
        }

        CreditCardApplication cca = _mapper.Map<CreditCardApplication>(applicationDetails);

        await _applicationRepository.AddAsync(cca);

        return View("ApplicationComplete", cca);
    }

    public IActionResult Error()
    {
        return View();
    }
}

I intially only had the line CreditCardApplication cca = _mapper.Map(applicationDetails);

but the comiler complained :

The type arguments for method 'IMapper.Map<TDestination>(object)' cannot be inferred from the usage. Try specifying the type arguments explicitly.  

This led to me trying to specify the type and at this point, it now returns null. Can anyone tell me what I doing wrong, please?

Adding class definitions below:

public class CreditCardApplication
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public decimal GrossAnnualIncome { get; set; }
    public string FrequentFlyerNumber { get; set; }
}



public class NewCreditCardApplicationDetails
{
    [Display(Name = "First Name")]
    [Required(ErrorMessage = "Please provide a first name")]
    public string FirstName { get; set; }

    [Display(Name = "Last Name")]
    [Required(ErrorMessage = "Please provide a last name")]
    public string LastName{ get; set; }

    [Display(Name = "Age (in years)")]
    [Required(ErrorMessage = "Please provide an age in years")]
    [Range(18,int.MaxValue, ErrorMessage = "You must be at least 18 years old")]
    public int? Age { get; set; }

    [Display(Name = "Gross Income")]
    [Required(ErrorMessage = "Please provide your gross income")]        
    public decimal? GrossAnnualIncome { get; set; }
    public string FrequentFlyerNumber { get; set; }
}