2017-01-11 59 views
0

我在我想通过和它在MVC控制器映射到定制的C#类angularjs一个对象。但每当我做这个类对象是完全null。从Angularjs传递对象到MVC的控制器和映射到类对象

$scope.Get = function() { 
     var EService = [{ 
      id: $scope.Id, 
      servicename: $scope.ServiceName, 
      servicetype: $scope.ServiceType, 
      monthlyrental: $scope.MonthlyRental, 
      serviceremarks: $scope.ServiceRemarks, 
      servicestatus: $scope.status, 
      activationdate: $scope.ActivationDate, 
      deactivationdate: $scope.DeActivationDate 
     }]; 

     $http.post('/TS/API/Insert', Service).then(function (res) { 
      debugger; 
     }) 

MVC控制器和类别:

[HttpPost] 
    public string Insert(ServicesMaster Service) 
    { 

     GIBCADBEntities gfientity = new GIBCADBEntities(); 

     var record = "Sent" 
     return Json(record, JsonRequestBehavior.AllowGet); 
    } public class ServicesMaster 
{ 
    public string id { set; get; } 
    public string servicename { set; get; } 
    public string servicetype { set; get; } 
    public int? monthlyrental { set; get; } 
    public string serviceremarks { set; get; } 
    public byte servicestatus { set; get; } 
    public DateTime? activationdate { set; get; } 
    public DateTime? deactivationdate { set; get; } 
} 

JavaScript变量/对象“电子服务”是确定在这里,和只透过ServicesMaster对象时与空值创建的并且没有数据被映射到它。我可以从这里发送单个字符串或任何值,但发送完整对象时,其行为如此。

回答

1

你传入从前端的阵列,并且从服务器端提取对象。只需将“[”和“]”大括号删除,同时将值设置为EService。像:

$scope.Get = function() { 
    var Service = {}; 
    Service = { 
     id: $scope.Id, 
     servicename: $scope.ServiceName, 
     servicetype: $scope.ServiceType, 
     monthlyrental: $scope.MonthlyRental, 
     serviceremarks: $scope.ServiceRemarks, 
     servicestatus: $scope.status, 
     activationdate: $scope.ActivationDate, 
     deactivationdate: $scope.DeActivationDate 
    }; 

    $http.post('/TS/API/Insert', Service).then(function (res) { 
     debugger; 

    }); 
}; 

它应该现在工作。 :)

相关问题