我通过axios.post请求将模型发送到我的后端服务器。都好。但是,我很难发送这些ViewModel的数组。
这是我到目前为止所得到的。
我的axios方法:
createMusicList() {
const { playlist } = this.state;
let model = {Name: "PlayListOne",
Tracks: ["test1", "test2"]}
console.log(playlist);
console.log(model);
axios({
url: 'http://localhost:60231/api/values',
method: 'post',
headers: {'content-type' : 'application.json'},
params: model
})
.then(function (response){
console.log(response.data)
})
.catch(function (error){
console.log(error);
});
}
这是我后端的ViewModel:
public class PlaylistModel
{
public string[] Tracks { get; set; }
public string Name { get; set; }
}
这是我后面的ActionMethod:
[HttpPost]
public void Post(PlaylistModel result)
{
Console.WriteLine();
}
这是继续传递数组的正确方法吗?如果我传递具有这些属性的多个对象,我是否应该在类型数组的ViewModel中创建所有属性?
编辑:为了简单和测试目的,我更改了代码。如果我将“model”发送到BackEnd,则会映射“Name”属性,但是,字符串数组不会 - 它返回类似{string [0]}的内容
终于明白了!
axios({
url: 'http://localhost:60231/api/values',
method: 'post',
data: qs.stringify(model),
})
在我的模型上使用“qs.stringify”以及“数据”作为关键。如果是params,则数据将作为查询字符串发送。