.Net Core controller to update array, but only with included properties

隐身守侯 提交于 2019-12-11 08:52:17

问题


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?


回答1:


The best solution I could come up with involves reading the request as a JArray rather than a List. I can then recurse and get the ID for each object. Get the object from the database and PopulateObject to just update the relevant properties. Looks something like this;

    [HttpPut("UpdateControlLinePointSet")]
    public async Task<IActionResult> UpdateControlLinePointSet([FromBody] JArray pointSetJson)
    {
        if (!ModelState.IsValid) return BadRequest(ModelState);
        foreach (JToken p in pointSetJson)
        {
            ControlLinePoint clp = _context.ControlLinePoint.First(x => x.ControlLinePointId == (int)p["ControlLinePointId"]);
            JsonConvert.PopulateObject(p.ToString(), clp);
            _context.Entry(clp).State = EntityState.Modified;
        }

        await _context.SaveChangesAsync();

        return NoContent();
    }


来源:https://stackoverflow.com/questions/50852571/net-core-controller-to-update-array-but-only-with-included-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!