AutoMapper目标对象设置为NULL(AutoMapper setting destinati

2019-09-26 10:47发布

我一直在这篇文章下面: https://medium.com/ps-its-huuti/how-to-get-started-with-automapper-and-asp-net-core-2-ecac60ef523f

但是,当我得到的实际映射部分我的目的地为空。 谁能告诉我,我做错了什么?

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>();
    }   
}

全ApplyController.cs,但我感兴趣的是HttpPost Index方法。

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();
    }
}

我intially只有行CreditCardApplication CCA = _mapper.Map(applicationDetails);

但comiler抱怨:

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

这导致我试图指定类型,并在这一点上,现在返回null。 谁能告诉我什么,我做错了吗?

下面的添加类定义:

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; }
}
文章来源: AutoMapper setting destination object to null