'Update an existing parent entity with multiple child entities in database ASP.NET MVC
I want to update my existing entities that consist a parent entity and two child entities.
Previously I have utilized view model to create data that include a parent class and two child class, however I could not use view model as parameter in update because it requires an integer parameter to know which Id I should update.
Relationship diagram:
Model classes:
public class Requisition
{
[Key]
public int requisitionId { get; set; }
public string Branch { get; set; }
public List<TravelAttachment> AllFiles { get; set; }
public List<Itinerary> Itineraries { get; set; } = new List<Itinerary>();
}
public class TravelAttachment
{
[Key]
public int AttachId { get; set; }
public string FilePath { get; set; }
public int? requisitionId { get; set; }
}
public class Itinerary
{
[Key]
public int ItineraryId { get; set; }
public string Departure { get; set; } = "";
public string Arrival { get; set; } = "";
public int? requisitionId { get; set; }
}
View model:
public class RequisitionViewModel
{
public int requisitionId { get; set; }
public string Branch { get; set; } = "";
public List<IFormFile> File { get; set; }
public List<Itinerary> Itineraries { get; set; } = new List<Itinerary>();
}
What I have tried is to create a view model object in my controller, and the outcome is telling me one of my variable in view model object get returned with null value.
Controller
public IActionResult UpdateRequisition(int id)
{
if (ModelState.IsValid)
{
var requisition = _context.Requisitions
.Include(r => r.Itineraries)
.Include(r => r.AllFiles)
.AsNoTracking()
.FirstOrDefault(m => m.requisitionId == id);
string uniqueFileName = null;
// have null object here
RequisitionViewModel model = new RequisitionViewModel();
List<TravelAttachment> attachments = new List<TravelAttachment>();
//problem that inform my model.File is null
foreach (IFormFile file in model.File)
{
string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "dist/files");
uniqueFileName = Guid.NewGuid().ToString() + "_" + file.FileName;
string filePath = Path.Combine(uploadsFolder, uniqueFileName);
file.CopyTo(new FileStream(filePath, FileMode.Create));
attachments.Add(new TravelAttachment { FilePath = uniqueFileName });
}
// using automapper to link viewmodel with model
var requisitionApplied = _mapper.Map<Requisition>(model);
requisitionApplied.AllFiles = attachments;
requisitionApplied.Itineraries = requisition.Itineraries;
_context.Requisitions.Update(requisitionApplied);
_context.SaveChanges();
return RedirectToAction("Index");
}
return View(id);
}
In this case, I am not able to upload new files in this action, what should I do to make sure user can upload new files?
Please ask me for additional information if you need more elaboration. I am appreciated for your kind support!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
