I am working on a project that requires a POST method for multicast Json, where the data is located in 5 different related tables with FK.
here is the Json output using GET:
{
"UserId": 1,
"Id": 1,
"ValueStoryName": "Value Story 101",
"Organization": "Charity Foundation",
"Industry": "Education",
"Location": "Singapore",
"AnnualRevenue": 1000,
"CreatedDate": "2017-07-27T00:00:00",
"ModifiedDate": "2017-01-01T00:00:00",
"BusinessValueToYou": [
{
"Id": 1,
"BVUSId": 1,
"BalanceSheet": 348,
"IncomeStatement": 546,
"ValueDriver": [
{
"BVUSId": 1,
"BVUId": 1,
"ValueDriver": "Give kind to others",
"Selected": true,
"TotalSavings": 0,
"SubLever": [
{
"BVUId": 1,
"BVUSVDId": 1,
"Item": "Help when needed",
"Selected": true,
"EstAnnualValue": 1,
"UserInput": [
{
"BVUSVDId": 1,
"BVUUIId": 1,
"Item": "Total Benefit",
"UserInput": 10
}
]
}
]
}
]
}
]
}
and what I have for my POST method under the controller:
[ResponseType(typeof(ValueStory))]
public async Task<IHttpActionResult> PostValueStory(ValueStory valueStory)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.ValueStories.Add(valueStory);
db.BusinessValueToYou.AddRange(valueStory.BusinessValueToYou);
await db.SaveChangesAsync();
return CreatedAtRoute("DefaultApi", new { id = valueStory.Id }, valueStory);
}
the above code only adds data for valuestory and businessvaluetoyou, but cannot add values for valuedriver, subvaluedriver and userinput. I hacked into the brain on how to do this job, but no luck. Any help and links would be greatly appreciated.
This is my BusinessValueToYou model:
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace AribaWebService.Models
{
public class BusinessValueToYou
{
public int Id { get; set; }
[Key]
public int BVUSId { get; set; }
public decimal BalanceSheet { get; set; }
public decimal IncomeStatement { get; set; }
public virtual ValueStory ValueStory { get; set; }
public List<BVUValueDriver> ValueDriver { get; set; }
}
public class BVUValueDriver
{
public int BVUSId { get; set; }
[Key]
public int BVUId { get; set; }
[Required]
public string ValueDriver { get; set; }
public bool Selected { get; set; }
public decimal TotalSavings { get; set;}
public virtual BusinessValueToYou BusinessValueToYou { get; set; }
public List<BVUSubValueDriver> SubLever { get; set; }
}
public class BVUSubValueDriver
{
public int BVUId { get; set; }
[Key]
public int BVUSVDId { get; set; }
[Required]
public string Item { get; set; }
public bool Selected { get; set; }
public decimal EstAnnualValue { get; set; }
public virtual BVUValueDriver BVUValueDriver { get; set; }
public List<BVUUserInput> UserInput { get; set; }
}
public class BVUUserInput
{
public int BVUSVDId { get; set; }
[Key]
public int BVUUIId { get; set; }
[Required]
public string Item { get; set; }
public double UserInput { get; set; }
public virtual BVUSubValueDriver BVUSubValueDriver { get; set; }
}
}
here is my ValueStory model:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace AribaWebService.Models
{
public class ValueStoryDetailDTO
{
public int UserId { get; set; }
[Key]
public int Id { get; set; }
public string ValueStoryName { get; set; }
public string Organization { get; set; }
public string Industry { get; set; }
public string Location { get; set; }
public string Currency { get; set; }
public double AnnualRevenue { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime ModifiedDate { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public virtual User User { get; set; }
public List<AreaOfInterest> AreaOfInterest { get; set; }
public List<BusinessValueToYou> BusinessValueToYou { get; set; }
public List<BusinessValueFromSap> BusinessValueFromSap { get; set; }
}
}