ASP.net MVC 5 从视图模型错误编辑数据库中的条目 - 实体类型 <myViewModel> 不是当前上下文模型的一部分

ASP.net MVC 5 Editing entry in the database from a view model error - The entity type <myViewModel> is not part of the model for the current context

我有一个从 ViewModel 填充的编辑页面。此 ViewModel 从几个模型(参与者、性别、国家/地区)中获取项目:

ViewModel

namespace MVCManageParticipants.Models
{
    public class ParticipantDetailsViewModel
    {
        public int Id { get; set; }
        public int SiteId { get; set; }
        public int Status { get; set; }
        public string Gender { get; set; }
        public string Title { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
        public int CountryId { get; set; }
        public string Country { get; set; }
        public string Postcode { get; set; }
        public string Telephone { get; set; }
        public string Notes { get; set; }

        public IEnumerable<Country> Countries { get; set; }
    }
}

IEnumerable Countries 返回一个完整的国家列表,并用数据库中的数据填充一个下拉列表(这是我想扩展的城市、性别、状态,但需要先让一个工作).

附带问题:我是如何做到这一点的,这是从数据库中填充视图下拉列表的公认方式吗?

页面在 [HttpGet] 上正常填充并将 ViewModel 发送回视图。

[HttpGet] 控制器

var model = (from p in _db.Participants
             where p.Id == id
             select new ParticipantDetailsViewModel
             {
                Id = p.Id,
                SiteId = p.SiteId,
                Status = p.Status,
                Gender = p.Gender.Name,
                Title = p.Title,
                Name = p.Name,
                City = p.City.Name,
                Country = p.Country.PrettyName,
                CountryId = p.Country.Id,
                Postcode = p.Postcode,
                Telephone = p.Telephone,
                Notes = p.Notes,
                Countries = _db.Countrys.ToList()
      }).FirstOrDefault();

[HttpPost] 控制器

public ActionResult Edit(ParticipantDetailsViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                _db.Entry(viewModel).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index", new { id = viewModel.Id });
            }
            return View(viewModel);
        }

_db.Entry(viewModel).State = EntityState.Modified;:

这行给我一个错误
An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: The entity type ParticipantDetailsViewModel is not part of the model for the current context.

参与者模型

public class Participant
    {
        public int Id { get; set; }
        public int SiteId { get; set; }
        public int Status { get; set; }
        public Gender Gender { get; set; }
        public string Title { get; set; }
        public string Name { get; set; }
        public City City { get; set; }
        public Country Country { get; set; }
        public string Postcode { get; set; }
        public string Telephone { get; set; }
        public string Notes { get; set; }
    }

这是因为我正在尝试更新 ViewModel 而不是 Participant 模型?我应该创建一个新的参与者对象并以这种方式更新传入数据吗?

ParticipantDetailsViewModel 不是 dbcontext 的一部分,您需要使用 ID 从数据库中获取 Participant 对象,并使用 viewModel 中的信息更新它:

public ActionResult Edit(ParticipantDetailsViewModel viewModel)
{
      if (ModelState.IsValid)
      {
           var participant = _db.Participants.FirstOrDefault(p => p.Id == viewModel.Id);

           //set all properties whith new values
           participant.SiteId = viewModel.SiteId

           _db.Entry(participant).State = EntityState.Modified;
           _db.SaveChanges();
           return RedirectToAction("Index", new { id = viewModel.Id });
      }
      return View(viewModel);
}