When I PUT to my controller for an update, I can use code like the following to ensure that only those properties that are specified in the object are updated. In other words, if I had a ControlLinePointDto object with properties ID, X, Y and Z, the following would only update property X
JSON
{
"key" : 5,
"values" : {
"X": 1234
}
}
Controller
[HttpPut]
public async Task<IActionResult> PutControlLinePoint(int key, string values)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
int id = key;
ControlLinePoint controlLinePoint = _context.ControlLinePoint.First(x => x.ControlLinePointId == key);
JsonConvert.PopulateObject(values, controlLinePoint);
if (id != controlLinePoint.ControlLinePointId) return BadRequest();
_context.Entry(controlLinePoint).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ControlLinePointExists(id)) return NotFound();
else throw;
}
return NoContent();
}
Now I want to do the same for an array of controllinepoints. I could create an object that was simply [{"key":5, "values":{"X": 1234}}], and deserialize it - then utilize my code per aboce, but this is starting to get pretty complex. Is there a better way?