1

I'm using ASP .Net Core. I have a DTO with 3 models of table from DB Scaffolding.

public class StudentDTO
{
   public int StudentId { get; set; }
   public Student StudentInfo { get; set; }
   public Address StudentAddressDetails { get; set; }
   public Extracurricular ExActivities { get; set; }
}

I'm able to Patch models using JsonPatchDocument with a separate route like the following:

public async Task<IActionResult> PatchStudent(int studentId, [FromBody] JsonPatchDocument<Student> student)

public async Task<IActionResult> PatchStudentAddress(int studentId, [FromBody] JsonPatchDocument<Address> studentAddress)

public async Task<IActionResult> PatchExActivities(int studentId, [FromBody] JsonPatchDocument<ExtraCurricular> studentExActivities)

I would like to patch in StudentDTO directly. I tried with

public async Task<IActionResult> PatchStudentDetails(int studentId, [FromBody] JsonPatchDocument updateStudentInfo)
{
    StudentDTO student = await GetStudentInfo(int studentId);

    updateStudentInfo.ApplyTo(student.StudentInfo);
    updateStudentInfo.ApplyTo(student.StudentAddressDetails); //lin 2
    updateStudentInfo.ApplyTo(student.ExActivities); //line 3
}

If I try to update StudentInfo, I get segment not found or propertyname not found on other two lines. It makes sense. How can I achieve this scenario with JsonPatchDocument? or is any other effective way to do this?

Thanks in Advance!

user2782405
  • 393
  • 1
  • 6
  • 20
  • maybe you can take a look at the link provided in the answer to this SO question https://stackoverflow.com/questions/55159208/jsonpatchdocument-onto-a-complex-entity-framework-tracked-object – akg179 Oct 24 '20 at 22:27
  • @akg179: Thanks for sharing the link! Even though it was a for a different scenario, it helped me! – user2782405 Oct 26 '20 at 17:29

0 Answers0