C#MVC AngularJS:调用了控制器方法,但没有从$http.post()方法接收值
我有一个Angular应用程序调用c#mvc方法: AngularJS:C#MVC AngularJS:调用了控制器方法,但没有从$http.post()方法接收值,c#,arrays,angularjs,asp.net-mvc,http-post,C#,Arrays,Angularjs,Asp.net Mvc,Http Post,我有一个Angular应用程序调用c#mvc方法: AngularJS: var data = JSON.stringify({ 'panelists': $scope.arr, 'webId': $scope.webinarId }); //Call the services $http.post('/Home/CreatePanelists', JSON.stringify(data)) .then(function (response) {
var data = JSON.stringify({ 'panelists': $scope.arr, 'webId': $scope.webinarId });
//Call the services
$http.post('/Home/CreatePanelists', JSON.stringify(data))
.then(function (response) {
if (response.data)
$scope.msg = 'Post Data Submitted Successfully!';
}, function (response) {
$scope.msg = 'Service not Exists';
});
数据内容:
{"panelists":[{"name":"ali","email":"ali@email.com"},{"name":"tom","email":"tom@email.com"},{"name":"arthur","email":"arthur@email.com"}],"webId":94395753244}
添加了以下文件:
/Models/Home/Panelist.cs
/Models/Home/Parameters.cs
当调试器进入CreatePanelists方法时,我添加了一个监视数据,panelists和webId都为null
我不明白问题出在哪里。当我在Chrome中调试AngularJS代码,并进入发出post请求的步骤时,我看到变量数据确实包含带有值的对象数组(如上所示)
为什么MVC控制器方法CreatePanelist没有看到这些值
如果有人有一个想法并且不介意提出,那就太好了
提前感谢您的帮助。您的问题来自于调用
JSON.stringify
两次。设置变量数据时,您已经在对其进行字符串化,请不要在post
中再次执行此操作
public class Panelist
{
public string name { get; set; }
public string email { get; set; }
}
public class Parameters
{
public IList<Panelist> panelists { get; set; }
public string webId { get; set; }
}
public ActionResult CreatePanelists(Parameters data)
{
System.Diagnostics.Debug.WriteLine(data.webId);
System.Diagnostics.Debug.WriteLine(data.panelists);
return new EmptyResult();
}