I have one payload like below to update via patch call in webAPI.
[
{
"value": [
{
"Id": "12",
"name": "ABC"
},
{
"Id": "89",
"name": "XYZ"
}
],
"path": "/basepathofemployee",
"op": "replace"
}
]
And my action method of controller is like and there I want to get the value of Id & name
public async Task<IActionResult> UpdateData([FromBody] JsonPatchDocument<EmployeeDocument> patchDoc)
{
// here I want to get value of Id (12, 89) & name (ABC, XYZ)
}
I tried to get the value from the path itself like,
var employee = patchDoc.Operations.Where(o => o.path.Equals("/basepathofemployee"));
its giving IEnumerable and if I loop through that I am not getting the actual value of id and name.
Can you pls guide me how to get the actual value of id and name?
if it needs patchdocument:
if (patchDoc != null)
{
var model = CreateModel(); //need to create instance
patchDoc.ApplyTo(model, ModelState);
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
else{
// model should have valid properties here
}
return new ObjectResult(model);
}
else
{
return BadRequest(ModelState);
}
should also have this decorator on the action:
[HttpPatch]
if can do without patchdocument:
just create a class:
public class Patch{
public List<Data> value {get;set;}
public string path {get;set;}
public string op {get;set;}
}
and another Data
public class data{
public int Id {get;set;}
public string name {get;set;}
}
then change signature to:
public async Task<IActionResult> UpdateData([FromBody] Patch patchDoc)
{
// here I want to get value of Id (12, 89) & name (ABC, XYZ)
// should be available patchDoc.value[0].Id / patchDoc.value[1].Id
}
might need:
public async Task<IActionResult> UpdateData([FromBody] List<Patch> patchDoc)
{
// here I want to get value of Id (12, 89) & name (ABC, XYZ)
// should be available patchDoc[0].value[0].Id / patchDoc[0].value[1].Id
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.