0

I am trying to code a PUT method for a multiple array but I am getting a NULL error.

this is my code inside my controller for valuestory where its child later is Area of Interest and AOI Value Driver:

// 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);
}

and this is the json parameter that I am trying to pass:

{
  "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
        }
      ]
    }
  ]
}

and the error that I am getting from postman is here https://ibb.co/jMHmuv

I can't fully understand what does the error mean but I hope that somebody can help me with this. revised controller code for the PUT method:

// 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);
}

revised Json input:

{
  "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
        }
      ]
    }
  ]
}
5
  • The error tells you to step into your method, and then step through it until you reach the position, where that error occurs. Chances are, your valueStory is null and we need to see your javascript code or how else you put that data up there. Commented Aug 14, 2017 at 8:13
  • You can paste that stack trace into visual studio from your clipboard using ctrl + E, ctrl + T. The error is on line 360 in ValueStoriesController.cs Commented Aug 14, 2017 at 8:15
  • @Max this is what I got [link]ibb.co/gJKVnF Commented Aug 14, 2017 at 8:34
  • Sorry, I didn't realise that this was a ReSharper only shortcut. Either way, the problem will be on line 360 (assuming that you were running in debug mode and haven't modified the code since). Commented Aug 14, 2017 at 11:05
  • @Max, i managed to get it working until AreaOfInterest. As for the AOIValueDrivers, its going thru and I can see from the debug mode that the values are getting read from the json parameters but its not saving to the DB table. Any idea what's wrong or missing in my code for the AOIValueDrivers? Commented Aug 15, 2017 at 2:29

1 Answer 1

1

I managed to get it working by putting additonal condition to my query for aoi and 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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.