0
我想爲多個數組編寫一個PUT方法,但我得到一個NULL錯誤。針對Json多個數組的C#Web API PUT方法
這是我的代碼我控制器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
我不能完全理解什麼是ER ror的意思,但我希望有人能幫助我。對於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
}
]
}
]
}
這個錯誤告訴你進入你的方法,然後遍歷它直到你到達發生錯誤的位置。機會是,您的valueStory爲null,我們需要查看您的JavaScript代碼或您如何將數據放在那裏。 – Marco
您可以使用'ctrl + E,ctrl + T'將剪輯板中的堆棧軌跡粘貼到Visual Studio中。錯誤是在ValueStoriesController.cs – Max
@ Max這是我得到的[鏈接] https://ibb.co/gJKVnF –