asp.netasp.net-mvcasp.net-mvc-4asp.net-mvc-viewmodel

insert data using view model class


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

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

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

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

I have to use the fields of MappedViewModels in public ActionResult Create(int? ProjectId).

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

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

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!!!


Solution

  • Update your action logic as below

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

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