Json 多数组的 C# Web API PUT 方法
C# Web API PUTmethod for Json Multiple Array
我正在尝试为多个数组编写 PUT 方法,但出现 NULL 错误。
这是我控制器中的代码,用于 valuestory,它的子项后来是感兴趣区域和 AOI 价值驱动因素:
// PUT: api/ValueStories/5
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> PutValueStory(int id, ValueStory valueStory)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != valueStory.Id)
{
return BadRequest();
}
//To update Value Story Item all the way to
//Area of interest and child layers
var vsid = id;
var vs = (from v in db.ValueStories where v.Id == vsid select v).FirstOrDefault();
//db.Entry(valueStory).State = EntityState.Modified;
if (vs == null)
{
return NotFound();
}
else
{
vs.Id = vsid;
vs.ModifiedDate = DateTime.Now;
//AreaOfInterest and AOIValueDriver START
foreach (var item in vs.AreaOfInterest)
{
var aoi = (from a in db.AreaOfInterests where a.Id == vs.Id select a).FirstOrDefault();
if (aoi != null)
{
aoi.AOIName = item.AOIName;
aoi.Selected = item.Selected;
aoi.Id = vs.Id;
//AOIValueDriver START
foreach (var item2 in aoi.AOIValueDrivers)
{
var aoivd = (from b in db.AOIValueDrivers where b.AOIId == aoi.AOIId select b).FirstOrDefault();
if (aoivd != null)
{
aoivd.Item = item2.Item;
aoivd.SubItem = item2.SubItem;
aoivd.Value = item2.Value;
}
}
//AOIValueDriver EMD
}
}
//AreaOfInterest and AOIValueDriver END
// --------------------------------//
}
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ValueStoryExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
这是我要传递的 json 参数:
{
"Id": 1,
"ValueStoryName": "Value Story 101",
"Organization": "Charity Foundation",
"Industry": "Education",
"Currency": "$ ",
"AnnualRevenue": 1000,
"AreaOfInterest": [
{
"Id": 1,
"AOIId": 1,
"AOIName": "Supply Chain/ Direct Materials",
"Selected": false,
"AOIValueDrivers": [
{
"AOIId": 1,
"AOIVDId": 1,
"Item": "Negotiate better prices & conditions",
"SubItem": "Automate the process of sourcing of direct materials and integrate it to you ERP and key execution systems",
"Value": 0
}
]
},
{
"Id": 1,
"AOIId": 2,
"AOIName": "Sourcing",
"Selected": true,
"AOIValueDrivers": [
{
"AOIId": 2,
"AOIVDId": 10,
"Item": "Negotiate better prices & conditions",
"SubItem": "Foster supplier competition to reduce pricing and obtain best market value",
"Value": 3
}
]
},
{
"Id": 1,
"AOIId": 3,
"AOIName": "Procurement",
"Selected": true,
"AOIValueDrivers": [
{
"AOIId": 3,
"AOIVDId": 23,
"Item": "Lower costs",
"SubItem": "Provide access to an electronic marketplace of pre-sourced goods and services",
"Value": 3
}
]
},
{
"Id": 1,
"AOIId": 4,
"AOIName": "Accounts Payable",
"Selected": true,
"AOIValueDrivers": [
{
"AOIId": 4,
"AOIVDId": 32,
"Item": "Lower costs",
"SubItem": "Pay on time",
"Value": 3
}
]
},
{
"Id": 1,
"AOIId": 5,
"AOIName": "Working Captila/ Treasury",
"Selected": true,
"AOIValueDrivers": [
{
"AOIId": 5,
"AOIVDId": 37,
"Item": "Free up working capital",
"SubItem": "Capture more early pay discounts and allow suppliers to dynamically select discount rate and payment terms",
"Value": 3
}
]
},
{
"Id": 1,
"AOIId": 6,
"AOIName": "Risk and Compliance",
"Selected": false,
"AOIValueDrivers": [
{
"AOIId": 6,
"AOIVDId": 42,
"Item": "Protect your revenue",
"SubItem": "Proactively monitor and predict supply risks in real time",
"Value": 3
}
]
}
]
}
我从邮递员那里得到的错误在这里
https://ibb.co/jMHmuv
我不能完全理解这个错误是什么意思,但我希望有人能帮助我解决这个问题。
PUT 方法的修改控制器代码:
// PUT: api/ValueStories/5
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> PutValueStory(int id, ValueStory valueStory)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != valueStory.Id)
{
return BadRequest();
}
//To update Value Story Item all the way to
//Area of interest and child layers
//Business value to you and child layers
//Business value from SAP and child layers
var vs = (from v in db.ValueStories where v.Id.Equals(id) select v).FirstOrDefault();
//db.Entry(valueStory).State = EntityState.Modified;
if (vs != null)
{
vs.ModifiedDate = DateTime.Now;
vs.ValueStoryName = valueStory.ValueStoryName;
vs.Organization = valueStory.Organization;
vs.Location = valueStory.Location;
vs.Industry = valueStory.Industry;
vs.Currency = valueStory.Currency;
vs.AnnualRevenue = valueStory.AnnualRevenue;
vs.MutualActionPlan = valueStory.MutualActionPlan;
vs.Id = valueStory.Id;
}
else
{
return NotFound();
}
//areaofinterest and aoivaluedriver start
foreach (var item in valueStory.AreaOfInterest)
{
var aoi = (from a in db.AreaOfInterests where a.Id == vs.Id select a).FirstOrDefault();
if (aoi != null)
{
aoi.Selected = item.Selected;
aoi.Id = vs.Id;
//aoi.AOIId = aoi.AOIId;
//aoivaluedriver start
foreach (var item2 in item.AOIValueDrivers)
{
var aoivd = (from b in db.AOIValueDrivers where b.AOIId == aoi.AOIId select b).FirstOrDefault();
if (aoivd != null)
{
aoivd.Value = item2.Value;
aoivd.AOIId = aoi.AOIId;
//aoivd.AOIVDId = aoivd.AOIVDId;
}
}
//aoivaluedriver emd
}
}
//areaofinterest and aoivaluedriver end
// --------------------------------//
//}
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ValueStoryExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
修改了Json输入:
{
"Id": 1,
"ValueStoryName": "Value Story 222",
"Organization": "ABC",
"Industry": 11,
"Location": "Singapore",
"Currency": "P",
"AnnualRevenue":212000,
"MutualActionPlan": "testing789",
"AreaOfInterest": [
{
"AOIId": 1,
"Selected": false,
"AOIValueDrivers": [
{
"AOIVDId": 1,
"Value": 1
},
{
"AOIVDId": 2,
"Value": 2
},
{
"AOIVDId": 3,
"Value": 1
},
{
"AOIVDId": 4,
"Value": 1
},
{
"AOIVDId": 5,
"Value": 1
},
{
"AOIVDId": 6,
"Value": 5
},
{
"AOIVDId": 7,
"Value": 1
},
{
"AOIVDId": 8,
"Value": 1
},
{
"AOIVDId": 9,
"Value": 3
}
]
}
]
}
我通过为 aoi 和 aoi 的查询添加附加条件来设法让它工作
//areaofinterest and aoivaluedriver START
foreach (var item in valueStory.AreaOfInterest)
{
var aoi = (from a in db.AreaOfInterests where a.Id == item.Id && a.AOIId == item.AOIId select a).FirstOrDefault();
if (aoi != null)
{
aoi.Selected = item.Selected;
//aoivaluedriver START
foreach (var item2 in item.AOIValueDrivers)
{
var aoivd = (from b in db.AOIValueDrivers where b.AOIId == item2.AOIId && b.AOIVDId == item2.AOIVDId select b).FirstOrDefault();
if (aoivd != null)
{
aoivd.Value = item2.Value;
}
}
//aoivaluedriver END
}
}
//areaofinterest and aoivaluedriver END
我正在尝试为多个数组编写 PUT 方法,但出现 NULL 错误。
这是我控制器中的代码,用于 valuestory,它的子项后来是感兴趣区域和 AOI 价值驱动因素:
// PUT: api/ValueStories/5
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> PutValueStory(int id, ValueStory valueStory)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != valueStory.Id)
{
return BadRequest();
}
//To update Value Story Item all the way to
//Area of interest and child layers
var vsid = id;
var vs = (from v in db.ValueStories where v.Id == vsid select v).FirstOrDefault();
//db.Entry(valueStory).State = EntityState.Modified;
if (vs == null)
{
return NotFound();
}
else
{
vs.Id = vsid;
vs.ModifiedDate = DateTime.Now;
//AreaOfInterest and AOIValueDriver START
foreach (var item in vs.AreaOfInterest)
{
var aoi = (from a in db.AreaOfInterests where a.Id == vs.Id select a).FirstOrDefault();
if (aoi != null)
{
aoi.AOIName = item.AOIName;
aoi.Selected = item.Selected;
aoi.Id = vs.Id;
//AOIValueDriver START
foreach (var item2 in aoi.AOIValueDrivers)
{
var aoivd = (from b in db.AOIValueDrivers where b.AOIId == aoi.AOIId select b).FirstOrDefault();
if (aoivd != null)
{
aoivd.Item = item2.Item;
aoivd.SubItem = item2.SubItem;
aoivd.Value = item2.Value;
}
}
//AOIValueDriver EMD
}
}
//AreaOfInterest and AOIValueDriver END
// --------------------------------//
}
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ValueStoryExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
这是我要传递的 json 参数:
{
"Id": 1,
"ValueStoryName": "Value Story 101",
"Organization": "Charity Foundation",
"Industry": "Education",
"Currency": "$ ",
"AnnualRevenue": 1000,
"AreaOfInterest": [
{
"Id": 1,
"AOIId": 1,
"AOIName": "Supply Chain/ Direct Materials",
"Selected": false,
"AOIValueDrivers": [
{
"AOIId": 1,
"AOIVDId": 1,
"Item": "Negotiate better prices & conditions",
"SubItem": "Automate the process of sourcing of direct materials and integrate it to you ERP and key execution systems",
"Value": 0
}
]
},
{
"Id": 1,
"AOIId": 2,
"AOIName": "Sourcing",
"Selected": true,
"AOIValueDrivers": [
{
"AOIId": 2,
"AOIVDId": 10,
"Item": "Negotiate better prices & conditions",
"SubItem": "Foster supplier competition to reduce pricing and obtain best market value",
"Value": 3
}
]
},
{
"Id": 1,
"AOIId": 3,
"AOIName": "Procurement",
"Selected": true,
"AOIValueDrivers": [
{
"AOIId": 3,
"AOIVDId": 23,
"Item": "Lower costs",
"SubItem": "Provide access to an electronic marketplace of pre-sourced goods and services",
"Value": 3
}
]
},
{
"Id": 1,
"AOIId": 4,
"AOIName": "Accounts Payable",
"Selected": true,
"AOIValueDrivers": [
{
"AOIId": 4,
"AOIVDId": 32,
"Item": "Lower costs",
"SubItem": "Pay on time",
"Value": 3
}
]
},
{
"Id": 1,
"AOIId": 5,
"AOIName": "Working Captila/ Treasury",
"Selected": true,
"AOIValueDrivers": [
{
"AOIId": 5,
"AOIVDId": 37,
"Item": "Free up working capital",
"SubItem": "Capture more early pay discounts and allow suppliers to dynamically select discount rate and payment terms",
"Value": 3
}
]
},
{
"Id": 1,
"AOIId": 6,
"AOIName": "Risk and Compliance",
"Selected": false,
"AOIValueDrivers": [
{
"AOIId": 6,
"AOIVDId": 42,
"Item": "Protect your revenue",
"SubItem": "Proactively monitor and predict supply risks in real time",
"Value": 3
}
]
}
]
}
我从邮递员那里得到的错误在这里 https://ibb.co/jMHmuv
我不能完全理解这个错误是什么意思,但我希望有人能帮助我解决这个问题。 PUT 方法的修改控制器代码:
// PUT: api/ValueStories/5
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> PutValueStory(int id, ValueStory valueStory)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != valueStory.Id)
{
return BadRequest();
}
//To update Value Story Item all the way to
//Area of interest and child layers
//Business value to you and child layers
//Business value from SAP and child layers
var vs = (from v in db.ValueStories where v.Id.Equals(id) select v).FirstOrDefault();
//db.Entry(valueStory).State = EntityState.Modified;
if (vs != null)
{
vs.ModifiedDate = DateTime.Now;
vs.ValueStoryName = valueStory.ValueStoryName;
vs.Organization = valueStory.Organization;
vs.Location = valueStory.Location;
vs.Industry = valueStory.Industry;
vs.Currency = valueStory.Currency;
vs.AnnualRevenue = valueStory.AnnualRevenue;
vs.MutualActionPlan = valueStory.MutualActionPlan;
vs.Id = valueStory.Id;
}
else
{
return NotFound();
}
//areaofinterest and aoivaluedriver start
foreach (var item in valueStory.AreaOfInterest)
{
var aoi = (from a in db.AreaOfInterests where a.Id == vs.Id select a).FirstOrDefault();
if (aoi != null)
{
aoi.Selected = item.Selected;
aoi.Id = vs.Id;
//aoi.AOIId = aoi.AOIId;
//aoivaluedriver start
foreach (var item2 in item.AOIValueDrivers)
{
var aoivd = (from b in db.AOIValueDrivers where b.AOIId == aoi.AOIId select b).FirstOrDefault();
if (aoivd != null)
{
aoivd.Value = item2.Value;
aoivd.AOIId = aoi.AOIId;
//aoivd.AOIVDId = aoivd.AOIVDId;
}
}
//aoivaluedriver emd
}
}
//areaofinterest and aoivaluedriver end
// --------------------------------//
//}
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ValueStoryExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
修改了Json输入:
{
"Id": 1,
"ValueStoryName": "Value Story 222",
"Organization": "ABC",
"Industry": 11,
"Location": "Singapore",
"Currency": "P",
"AnnualRevenue":212000,
"MutualActionPlan": "testing789",
"AreaOfInterest": [
{
"AOIId": 1,
"Selected": false,
"AOIValueDrivers": [
{
"AOIVDId": 1,
"Value": 1
},
{
"AOIVDId": 2,
"Value": 2
},
{
"AOIVDId": 3,
"Value": 1
},
{
"AOIVDId": 4,
"Value": 1
},
{
"AOIVDId": 5,
"Value": 1
},
{
"AOIVDId": 6,
"Value": 5
},
{
"AOIVDId": 7,
"Value": 1
},
{
"AOIVDId": 8,
"Value": 1
},
{
"AOIVDId": 9,
"Value": 3
}
]
}
]
}
我通过为 aoi 和 aoi 的查询添加附加条件来设法让它工作
//areaofinterest and aoivaluedriver START
foreach (var item in valueStory.AreaOfInterest)
{
var aoi = (from a in db.AreaOfInterests where a.Id == item.Id && a.AOIId == item.AOIId select a).FirstOrDefault();
if (aoi != null)
{
aoi.Selected = item.Selected;
//aoivaluedriver START
foreach (var item2 in item.AOIValueDrivers)
{
var aoivd = (from b in db.AOIValueDrivers where b.AOIId == item2.AOIId && b.AOIVDId == item2.AOIVDId select b).FirstOrDefault();
if (aoivd != null)
{
aoivd.Value = item2.Value;
}
}
//aoivaluedriver END
}
}
//areaofinterest and aoivaluedriver END