使用视图模型类插入数据 [英] insert data using view model class

查看:97
本文介绍了使用视图模型类插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模型类::

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

查看模型类::

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页面.现在,当我尝试提交表单时,出现错误:

I have created the Create.cshtml page for MappedModels using MappedViewModels. Now when I try to submit the form, I am getting error::

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'.

我的创建控制器方法::

My Create controller method::

[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)

If I change the method to public ActionResult Create( MappedViewModels mappedModels), then getting error in db.Mappeds.Add(mappedModels)

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

此外,我的MappedViewModels不包含属性MappedId,如果我将其保留在(MappedViewModels)中,那么MappedId如何自动递增.

Also, my MappedViewModels does not contain attribute MappedId, if I keep it there (MappedViewModels) also how can MappedId be auto increment.

那么如何使用MappedViewModelsMappedModels插入数据?

So how to insert data using MappedViewModels to MappedModels?

根据解决方案中的建议,我尝试了::

As suggested in solution I tried ::

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类引起的.请帮忙!!!

But getting error Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.. It is causing because of Project class where there are multiple required fields. Please help!!!

推荐答案

如下更新您的操作逻辑

[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")]的用途是什么,我认为这不是必需的.我没用过.

What is the use of [Bind(Include = "MappedType,MappedAmount,ProjectId")] I don't think that is required. I never used it.

这篇关于使用视图模型类插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆