繁体   English   中英

用于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和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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM