如何 POST 包含从 AngularJS 到 ASP.NET MVC 控制器的字符串列表的对象?
How to POST an Object that contains a String list from AngularJS to ASP.NET MVC Controller?
我有以下 javascript 对象:
var param = {
FilePaths: ['String1', 'String2', 'String3'],
Share: false
}
我的AngularJSPOST如下:
$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
属性 通过得很好,但 FilePaths
是 Null
。我在这里做错了什么?我尝试使用 data: JSON.stringify(param)
,但仍然通过 Null
。
1.Add 下面 class FilePaths-
public class FilePaths
{
public List<string> Path { get; set; }
}
2.Mention 文件路径,作为 ImportParam class-
中的 属性
public class ImportParam
{
public FilePaths filePaths { get; set; }
public bool Share { get; set; }
}
3.Decorate 您的 json 如下所示,并将其分配给参数变量-
{
{"ImportParam":{
"filePaths":{"Path":["String1","String2","String3"]},
"Share": false}}
}
我有以下 javascript 对象:
var param = {
FilePaths: ['String1', 'String2', 'String3'],
Share: false
}
我的AngularJSPOST如下:
$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
属性 通过得很好,但 FilePaths
是 Null
。我在这里做错了什么?我尝试使用 data: JSON.stringify(param)
,但仍然通过 Null
。
1.Add 下面 class FilePaths-
public class FilePaths
{
public List<string> Path { get; set; }
}
2.Mention 文件路径,作为 ImportParam class-
中的 属性public class ImportParam
{
public FilePaths filePaths { get; set; }
public bool Share { get; set; }
}
3.Decorate 您的 json 如下所示,并将其分配给参数变量-
{
{"ImportParam":{
"filePaths":{"Path":["String1","String2","String3"]},
"Share": false}}
}