使用视图模型插入数据 class

insert data using view model class

型号class::

public class MappedModels
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int MappedId { get; set; }
        public int MappedType { get; set; }
        public decimal MappedAmount { get; set; }
        
        [ForeignKey("ProjectId")]
        public ProjectModels Project { get; set; } 
        public int ProjectId { get; set; }
    }

查看模型Class::

public class MappedViewModels
    {
        public int MappedType { get; set; }
        public decimal MappedAmount { get; set; }
        public int ProjectId { get; set; }
        public string ProjectName { get; set; }
        
        // these two fields only in MappedViewModels for calculation purpose
        public decimal ProjectAllocatedAmount { get; set; } 
        public decimal RemainingAmount { get; set; }  
}

我使用 MappedViewModels 为 MappedModels 创建了 Create.cshtml 页面。现在,当我尝试提交表单时,出现错误::

The model item passed into the dictionary is of type 'myapp.Models.MappedModels', but this dictionary requires a model item of type 'myapp.ViewModels.MappedViewModels'.

我的创建控制器方法::

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "MappedId,MappedType,MappedAmount,ProjectId")] MappedModels mappedModels)
{
    if (ModelState.IsValid)
    {
        db.Mappeds.Add(mappedModels);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(mappedModels);
}

如果我将方法更改为 public ActionResult Create( MappedViewModels mappedModels),则会在 db.Mappeds.Add(mappedModels)

中出现错误

我必须在 public ActionResult Create(int? ProjectId) 中使用 MappedViewModels 的字段。

另外,我的 MappedViewModels 不包含属性 MappedId,如果我把它放在那里 (MappedViewModels) 还有 MappedId 怎么会 自动递增 .

那么如何使用MappedViewModels插入数据到MappedModels

按照我尝试过的解决方案中的建议::

public ActionResult Create([Bind(Include = "MappedType,MappedAmount,ProjectId")] MappedViewModels model)
{
    if (ModelState.IsValid)
    {
        var dbMappedModel = new MappedModels
        {
          MappedType = model.MappedType,
          MappedAmount = model.MappedAmount,
          ProjectId = model.ProjectId
        }

        db.Mappeds.Add(dbMappedModel);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(mappedModels);
}

但是出现错误 Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.。这是由于 Project class 导致的,其中有多个必填字段。请帮忙!!!

更新您的操作逻辑如下

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "MappedType,MappedAmount,ProjectId")] MappedViewModels model)
{
    if (ModelState.IsValid)
    {
        // when you add to the context, create a new MappedModels object
        // You need not to worry about the MappedId, it will be auto incremented
        var dbMappedModel = new MappedModels
        {
          MappedType = model.MappedType,
          MappedAmount = = model.MappedAmount,
        
          Project = new ProjectModels { ... }
        }

        db.Mappeds.Add(dbMappedModel);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(mappedModels);
}

[Bind(Include = "MappedType,MappedAmount,ProjectId")]有什么用我觉得不需要。我没用过。