inserire i dati utilizzando la classe del modello di visualizzazione

Aug 20 2020

Classe modello:

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

Visualizza classe modello:

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

Ho creato la Create.cshtmlpagina per l' MappedModelsutilizzo di MappedViewModels. Ora quando provo a inviare il modulo, ricevo l'errore:

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

Il mio metodo di controller di creazione:

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

Se cambio il metodo in public ActionResult Create( MappedViewModels mappedModels), viene visualizzato un erroredb.Mappeds.Add(mappedModels)

Devo usare i campi di MappedViewModelsin public ActionResult Create(int? ProjectId).

Inoltre, il mio MappedViewModelsnon contiene attributo MappedId, se lo tengo lì (MappedViewModels) anche come può MappedIdessere l'incremento automatico .

Allora come inserire i dati usando MappedViewModelsa MappedModels?

Come suggerito nella soluzione ho provato:

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

Ma ottenendo errore Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.. Sta causando a causa della classe Project in cui sono presenti più campi obbligatori. Per favore aiuto!!!

Risposte

1 SowmyadharGourishetty Aug 20 2020 at 10:37

Aggiorna la tua logica di azione come di seguito

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

A cosa serve [Bind(Include = "MappedType,MappedAmount,ProjectId")]non credo che sia richiesto. Non l'ho mai usato.