Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json多数组的C#Web API POST方法_C#_Arrays_Json_Entity Framework_Asp.net Web Api2 - Fatal编程技术网

Json多数组的C#Web API POST方法

Json多数组的C#Web API POST方法,c#,arrays,json,entity-framework,asp.net-web-api2,C#,Arrays,Json,Entity Framework,Asp.net Web Api2,我正在从事一个项目,该项目需要一个多数组Json的POST方法,其中数据位于5个不同的FK相关表上 以下是使用GET的Json输出: { "UserId": 1, "Id": 1, "ValueStoryName": "Value Story 101", "Organization": "Charity Foundation", "Industry": "Education", "Location": "Singapore", "AnnualRevenue": 100

我正在从事一个项目,该项目需要一个多数组Json的POST方法,其中数据位于5个不同的FK相关表上

以下是使用GET的Json输出:

{
  "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
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}
我目前在控制器下的POST方法是:

// POST: api/ValueStories
[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);
}
//POST:api/ValueStories
[响应类型(类型(ValueStory))]
公共异步任务PostValueStory(ValueStory ValueStory)
{
如果(!ModelState.IsValid)
{
返回请求(ModelState);
}
db.ValueStories.Add(valueStory);
db.BusinessValueToYou.AddRange(valueStory.BusinessValueToYou);
等待db.saveChangesSync();
返回CreatedAtRoute(“DefaultApi”,新的{id=valueStory.id},valueStory);
}
上面的代码仅为valuestory和businessvaluetoyou添加数据,但无法为valuedriver、subvaluedriver和userinput添加值。我一直在绞尽脑汁想如何使这项工作成功,但运气不佳。任何帮助和推荐都将不胜感激

这是我的业务价值对您的模式:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace AribaWebService.Models
{
    public class BusinessValueToYou
    {
        // Foreign Key
        public int Id { get; set; }
        [Key]
        public int BVUSId { get; set; }
        public decimal BalanceSheet { get; set; }
        public decimal IncomeStatement { get; set; }

        // Navigation property
        public virtual ValueStory ValueStory { get; set; }

        public List<BVUValueDriver> ValueDriver { get; set; }

    }
    public class BVUValueDriver
    {
        // Foreign Key
        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;}

        // Navigation property
        public virtual BusinessValueToYou BusinessValueToYou { get; set; } 

        public List<BVUSubValueDriver> SubLever { get; set; }
        }

    public class BVUSubValueDriver
    {
        // Foreign Key
        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; }


        // Navigation property
        public virtual BVUValueDriver BVUValueDriver { get; set; }

        public List<BVUUserInput> UserInput { get; set; }

    }

    public class BVUUserInput
    {
        // Foreign Key
        public int BVUSVDId { get; set; }
        [Key]
        public int BVUUIId { get; set; }
        [Required]
        public string Item { get; set; }
        public double UserInput { get; set; }

        // Navigation property
        public virtual BVUSubValueDriver BVUSubValueDriver { get; set; }

    }
}
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
命名空间AribaWebService.Models
{
公务舱商业价值观
{
//外键
公共int Id{get;set;}
[关键]
public int BVUSId{get;set;}
公共十进制余额表{get;set;}
公共十进制非计算语句{get;set;}
//导航属性
公共虚拟ValueStory ValueStory{get;set;}
公共列表值驱动程序{get;set;}
}
公共类bvValue驱动程序
{
//外键
public int BVUSId{get;set;}
[关键]
public int BVUId{get;set;}
[必需]
公共字符串值驱动程序{get;set;}
已选择公共布尔值{get;set;}
公共十进制TotalSaves{get;set;}
//导航属性
公共虚拟业务价值ToYou业务价值ToYou{get;set;}
公共列表子版本{get;set;}
}
公共类bvusubvalue驱动程序
{
//外键
public int BVUId{get;set;}
[关键]
public int BVUSVDId{get;set;}
[必需]
公共字符串项{get;set;}
已选择公共布尔值{get;set;}
公共十进制设置值{get;set;}
//导航属性
公共虚拟bvValueDriver bvValueDriver{get;set;}
公共列表用户输入{get;set;}
}
公共类BVUUserInput
{
//外键
public int BVUSVDId{get;set;}
[关键]
public int BVUUIId{get;set;}
[必需]
公共字符串项{get;set;}
公共双用户输入{get;set;}
//导航属性
公共虚拟BVUSubValueDriver BVUSubValueDriver{get;set;}
}
}
以下是我的ValueStory模型:

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
    {
        // Foreign Key
        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; }

        // Navigation property
        [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; }


    }
}
使用Newtonsoft.Json;
使用制度;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.Linq;
使用System.Web;
命名空间AribaWebService.Models
{
公共类ValueStoryDetailDTO
{
//外键
public int UserId{get;set;}
[关键]
公共int Id{get;set;}
公共字符串ValueStoryName{get;set;}
公共字符串组织{get;set;}
公共字符串行业{get;set;}
公共字符串位置{get;set;}
公共字符串货币{get;set;}
公共双年度收入{get;set;}
公共日期时间CreatedDate{get;set;}
公共日期时间修改日期{get;set;}
//导航属性
[JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
公共虚拟用户用户{get;set;}
感兴趣的公共列表区域{get;set;}
公共列表BusinessValueToYou{get;set;}
公共列表BusinessValueFromSap{get;set;}
}
}

通常我们不会直接在web api中公开真实的Db实体,只需使用视图模型或DTO返回我们需要的内容,以防止过度暴露

您可以更改get方法以在单个模型中返回所需的内容

了解有关自动映射器的详细信息

构建更好的RESTful api:

对于您的问题,这是一个如何读取多个数组的示例:

// POST: api/ValueStories
public async Task<IHttpActionResult> PostValueStory(AbcViewModel viewModel)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    //Manual map or use Automapper here
    var entity = Mapper.Map<ValueStory>(viewModel);
    db.ValueStories.Add(entity);
    //db.BusinessValueToYou.AddRange(entity.BusinessValueToYou); // can map with automapper

    await db.SaveChangesAsync();

    return CreatedAtRoute("DefaultApi", new { id = entity .Id }, entity );
}

public class AbcViewModel
{
    public int UserId { get; set; }
    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 AnnualRevenue { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime ModifiedDate { get; set; }
    public IEnumerable<BusinessValueToYouViewModel> BusinessValueToYou { get; set; }

}

public class BusinessValueToYouViewModel
{
    public int Id { get; set; }
    public int BVUSId { get; set; }
    public int BalanceSheet { get; set; }
    public int IncomeStatement { get; set; }
    public IEnumerable<ValueDriverViewModel> ValueDriver { get; set; }
}

public class ValueDriverViewModel
{
    public int Id { get; set; }
    public int BVUSId { get; set; }
    public int BVUId { get; set; }
    public string ValueDriver { get; set; }
    public bool Selected { get; set; }
    public int TotalSavings { get; set; }
}
//POST:api/ValueStories
公共异步任务PostValueStory(AbcViewModel viewModel)
{
如果(!ModelState.IsValid)
{
返回请求(ModelState);
}
//手动映射或在此处使用自动映射
var entity=Mapper.Map(viewModel);
db.ValueStories.Add(实体);
//db.BusinessValueToYou.AddRange(entity.BusinessValueToYou);//可以使用automapper进行映射
等待db.saveChangesSync();
返回CreatedAtRoute(“DefaultApi”,新的{id=entity.id},entity);
}
公共类AbcViewModel
{
public int UserId{get;set;}
公共int Id{get;set;}
公共字符串ValueStoryName{get;set;}
公共字符串组织{get;set;}
公共字符串行业{get;set;}
公共字符串位置{get;set;}
公共字符串AnnualRevenue{get;set;}
公共日期时间CreatedDate{get;set;}
公共日期时间修改日期{get;set;}
public IEnumerable BusinessValueToYou{get;set;}
}
公共类BusinessValueToYouViewModel
{
公共int Id{get;set;}
public int BVUSId{get;set;}
公共整数平衡表{get;set;}
public int IncomeStatement{get;set;}
public IEnumerable ValueDriver{get;set;}
}
公共类ValueDriverViewModel
{
公共int Id{get;set;}
public int BVUSId{get;set;}
public int BVUId{get;set;}
公共字符串值驱动程序{get;set;}
已选择公共布尔值{get;set;}
公共整数TotalSaves{get;set;}
}

我使用AddRange成功地让它工作。谢谢您的帮助。

您所说的“无法添加值…”是什么意思?是ValueStory和BusinessValue吗