编辑操作未在自动映射器上执行任何操作

Edit action is not doing anything on automapper

我正在使用 Visual Studio 2019、.net core 3.1 和 automapper。我的编辑操作不编辑记录。我看过教程,但所有教程都只是一个动作,我需要做一个简单的操作。以我所做的常规编辑操作为例:

public class CustomerCountriesController : Controller
    {
        private readonly ApplicationDbContext _context;
        private readonly IMapper _mapper;

        public CustomerCountriesController(ApplicationDbContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        // GET: CustomerCountries
        public async Task<IActionResult> Index()        
        {
            //CustomerCountries customerCountry = new CustomerCountries();
            var customerCountry = await _context.CustomerCountries.ToListAsync();            
            List<CustomerCountriesDto> countries = _mapper.Map<List<CustomerCountries>, 
                                                    List<CustomerCountriesDto>>(await _context.CustomerCountries.ToListAsync());
         
            return View(countries);
        }

public async Task<IActionResult> Edit(string id)
        {
            if (id == null)
            {
                return NotFound();
            }
            
            var customerCountries = await _context.CustomerCountries.FindAsync(id);
            var model = _mapper.Map<CustomerCountries, CustomerCountriesDto>(customerCountries);

            if (customerCountries == null)
            {
                return NotFound();
            }

            

            return View(model);
            //return View(customerCountries);
        }

        // POST: CustomerCountries/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to, for 
        // more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]        
        //public async Task<IActionResult> Edit(string id, [Bind("CustomerCountry")] CustomerCountries customerCountries)
        public async Task<IActionResult> Edit(string customerCountry, CustomerCountriesDto customerCountriesDto)
        {
            if (customerCountry != customerCountriesDto.CustomerCountry)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var CustomerCountries = _mapper.Map<CustomerCountriesDto, CustomerCountries>(customerCountriesDto);                    
                    _context.Update(CustomerCountries);
                    await _context.SaveChangesAsync();                    
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CustomerCountriesExists(customerCountriesDto.CustomerCountry))                    
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(customerCountriesDto);
        }


public class AutoMapping : Profile
    {
        public AutoMapping()
        {
            CreateMap<CustomerCountries, CustomerCountriesDto>();
            CreateMap<CustomerCountriesDto, CustomerCountries>();
        }
    }

public class CustomerCountries
    {
        [StringLength(50, ErrorMessage = "Longitud máxima para el país: 50")]
        public string CustomerCountry { get; set; }

        public ICollection<CustomerRegions> CustomerRegions { get; set; }
    }

public class CustomerCountriesDto
    {
        public string CustomerCountry { get; set; }
    }

启动时

services.AddAutoMapper(typeof(Startup));

table 的 ID 是 CustomerCounty

你能告诉我正确的方法吗?

感谢一位经验丰富的开发人员带领我解决问题并不时更正我的代码,我找到了解决方案(这实际上真的帮助了大家)。原来我使用的是一个字段作为 PK:CustomerCountry...我没有使用和 Id,当我更改模型时,更新发生了

var CustomerCountries = _mapper.Map<CustomerCountriesDto, CustomerCountries>(customerCountriesDto);
var country = _context.CustomerCountries.FirstOrDefault(c => c.Id == CustomerCountries.Id);
country.CustomerCountry = customerCountriesDto.CustomerCountry;
                        _context.Update(country);
                    await _context.SaveChangesAsync();