Asp.net mvc 使用AngularJS将数据发布到MVC2.0
我在Service.js上的AngularJs Post请求:Asp.net mvc 使用AngularJS将数据发布到MVC2.0,asp.net-mvc,angularjs,Asp.net Mvc,Angularjs,我在Service.js上的AngularJs Post请求: this.Add = function (employee) { var response = $http({ method: "post", url: "/Employee/Add", data: JSON.stringify(employee), dataType: "json", });
this.Add = function (employee) {
var response = $http({
method: "post",
url: "/Employee/Add",
data: JSON.stringify(employee),
dataType: "json",
});
return response;
}
我的模态类:
[Serializable]
public class Employee
{
public Employee()
{ }
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
添加EmployeeController的方法:
[HttpPost]
public string Add(Employee employeeNew)
{
if (employeeNew != null)
{
unitOfWork.EmployeeRepository.Insert(employeeNew);
unitOfWork.Save();
return "Record has been Added";
}
else
{
return "Record has Not been Verified";
}
}
我使用MVC2.0。当我提出上述请求时,Add方法上的Employee类为空。另一方面,当我查看add请求时,我看到该请求具有FirstName、LastName、UserName和Password值,并且包含完整的正确数据。如何将这些属性绑定到on Add方法的Employee对象。$http是一个异步请求。这意味着此请求的返回由promis组成,这是在请求完成并且方法收到数据后可以执行的操作。你必须使用回调来处理这个问题。尝试:
this.Add = function (employee) {
var response = {}
$http({
method: "post",
url: "/Employee/Add",
data: JSON.stringify(employee),
dataType: "json",
}).succes(function(data){
response = data;
}).error(function(){
//Handle errors caused by the HTTP-request
})
}
我可以这样做,但我的问题是将PostValues绑定为Employee类对象。我的角度控制器文件是这样的$scope.Save=function(){var Employee={FirstName:$scope.FirstName,LastName:$scope.LastName,UserName:$scope.UserName,Password:$scope.Password};var getMSG=angularService.Add(Employee);我也有同样的问题..但我从
.apsx
页面.cshtml
页面切换。它解决了我的问题,我知道这样做不合适..但这仍然是一种解决方法