【问题标题】:How to POST an Object that contains a String list from AngularJS to ASP.NET MVC Controller?如何将包含来自 AngularJS 的字符串列表的对象发布到 ASP.NET MVC 控制器?
【发布时间】:2020-08-25 05:17:39
【问题描述】:

我有以下 javascript 对象:

var param = {
  FilePaths: ['String1', 'String2', 'String3'],
  Share: false
}

我的 AngularJS POST 如下:

$http({
  method: 'POST',
  url: config.apiUrl + '/ImportSubset/Import',
  data: param,
  headers: {
    'Content-Type': 'application/json'
  }
}).then(function (response) {
  console.log(response);
}, function (error) {
  console.log(error);
});

我的 ASP.NET 控制器:

public class ImportSubsetController : Controller
{
  public class ImportParam
  {
    public List<string> FilePaths;
    public bool Share;
  }

  public JsonResult Import(ImportParam param)
  {
    (...)
  }

当我调用导入 API 时,Share 属性通过就好了,但 FilePathsNull。我在这里做错了什么?我尝试使用data: JSON.stringify(param),但仍然以Null 传递。

【问题讨论】:

    标签: asp.net angularjs asp.net-mvc asp.net-core asp.net-mvc-5


    【解决方案1】:

    1.添加以下类FilePaths-

    public class FilePaths
      {
        public List<string> Path { get; set; }
    
      }
    

    2.提及 FilePaths ,作为 ImportParam 类中的属性-

    public class ImportParam
      {
        public FilePaths filePaths { get; set; }
        public bool Share { get; set; }
      }
    

    3.如下所示装饰您的 json,并将其分配给参数变量-

    {
      {"ImportParam":{
    
     "filePaths":{"Path":["String1","String2","String3"]},
    
      "Share": false}}
    }
    

    【讨论】:

    • 添加 '{ get;放; }' 到我的 'ImportParam' 类。解决了这个问题。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    • 2018-01-06
    相关资源
    最近更新 更多