AutoMapper将目标对象设置为null

时间:2019-03-22 14:48:56

标签: c# asp.net-core automapper

我一直在关注这篇文章: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();
    }
}

我最初只有线     CreditCardApplication cca = _mapper.Map(applicationDetails);

但编译器抱怨:

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

0 个答案:

没有答案