Json多数组的C#Web API方法

Json多数组的C#Web API方法,c#,json,entity-framework,api,put,C#,Json,Entity Framework,Api,Put,我试图为多重数组编写一个PUT方法,但得到一个空错误 这是我的valuestory控制器中的代码,其子级后面是感兴趣的区域和AOI值驱动程序: // PUT: api/ValueStories/5 [ResponseType(typeof(void))] public async Task<IHttpActionResult> PutValueStory(int id, ValueStory valueStory) { if (!ModelState.IsValid)

我试图为多重数组编写一个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);
}
我从邮递员那里得到的错误就在这里

我不能完全理解这个错误是什么意思,但我希望有人能帮我解决这个问题。 修改了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);
}

我通过在aoi和aoivd的查询中添加附加条件,成功地使其工作

    //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

错误会告诉您进入方法,然后逐步执行,直到到达发生错误的位置。很可能,您的valueStory为空,我们需要查看您的javascript代码或您如何将数据放在那里。您可以使用
ctrl+E、ctrl+T
从剪贴板将堆栈跟踪粘贴到visual studio中。错误出现在ValueStoriesController的第360行。cs@Max这就是我得到的[link]对不起,我不知道这是一个只有重新竖琴的捷径。不管是哪种方式,问题都会出现在第360行(假设您是在调试模式下运行的,并且从那以后就没有修改过代码)。@Max,我设法让它工作到感兴趣的区域。至于AOIValueDrivers,它正在运行,我可以从调试模式中看到,这些值是从json参数读取的,但没有保存到DB表中。你知道我的AOIValueDrivers代码有什么错误或遗漏吗?
{
  "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
        }
      ]
    }
  ]
}
    //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