2016-11-22 130 views
0

我有一个类如何通过类对象angularjs功能

public class Customer 
{ 
    private int _Id; 
    public int Id 
    { 
     get { return _Id; } 
     set { _Id = value; } 
    } 
    private String _Name; 
    public String Name 
    { 
     get { return _Name; } 
     set { _Name = value; } 
    } 
    private String _Address; 
    public String Address 
    { 
     get { return _Address; } 
     set { _Address = value; } 
    }  
} 

我需要使用这个类来保存数据。我需要知道如何调用这个类中的数据功能

$scope.Save = function() { 
      var httpreq = { 
       method: 'POST', 
       url: 'ajs.aspx/Save', 
       headers: { 
        'Content-Type': 'application/json; charset=utf-8', 
        'dataType': 'json' 
       }, 
       data: { //** Here How to call assign object value for my customer class **// } 
      } 
      $http(httpreq).success(function (response) { 
       alert("Saved successfully."); 
      }) 
     }; 

如何创建类对象,并为我的客户类在这个数据部分指定值。

在我的Web方法

[System.Web.Services.WebMethod()] 
    public static void Save(Customer objcsr) 
    { 
     using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString)) 
     { 
      using (SqlCommand cmd = new SqlCommand()) 
      { 
       cmd.Connection = con; 
       cmd.CommandText = "insert into customer (Name, Address) values (@Name, @Address);"; 
       cmd.Parameters.AddWithValue("@Name", objcsr.Name); 
       cmd.Parameters.AddWithValue("@Address", objcsr.Address); 
       con.Open(); 
       cmd.ExecuteNonQuery(); 
       con.Close(); 
      } 
     } 
    } 
+1

可能重复的[从Angular发布到.net WebAPI](http://stackoverflow.com/questions/16621706/posting-from-angular-to-net-webapi) –

回答

0

您需要用类属性通过JSON格式的数据,试试这个下面的代码

Var objcsr={Id:"1",Name:"Donald Trump",Address:"Alien Planet"} 

$scope.Save = function() { 
      var httpreq = { 
       method: 'POST', 
       url: 'ajs.aspx/Save', 
       headers: { 
        'Content-Type': 'application/json; charset=utf-8', 
        'dataType': 'json' 
       }, 
       data: objcsr 
      } 
      $http(httpreq).success(function (response) { 
       alert("Saved successfully."); 
      }) 
     }; 
0

当你从客户端发送数据到服务器,根据最佳做法,数据应以XMLJSON的形式发送。

因此,您的类对象的JSON可能是像

var obj = { 
    "id" : "YOUR ID", 
    "name" : "YOUR NAME", 
    "address" : "YOUR ADDRESS" 
} 

此外,根据最佳实践,HTTP请求应使用厂家或服务进行。

所以,你的工厂应该是这样

angular.module("YOUR_MODULE").factory('YOUR_SERVICE',function($http,$q){ 

    var obj = {}; 
    obj.saveObject = function(data){ 
     var defer = $q.defer(); 
     $http.post("YOUR URL",data).then(function(response){ 
     defer.resolve(response); 
     },function(error){ 
     defer.reject(error); 
     }); 
     return defer.promise; 

    } 
    return obj; 
}); 

就这样你可以做一个控制器调用作为

$scope.Save = function (data) { 
    YOUR_SERVICE.saveObject(data); 
} 

,并且可以被称为像

var obj = { 
     "id" : "YOUR ID", 
     "name" : "YOUR NAME", 
     "address" : "YOUR ADDRESS" 
    } 
    $scope.Save(obj); 
0

如果你获得它从表单元素那么你应该使用它像

$scope.Save = function() { 
      var customerObject={ 
           "Id": $scope.customerId, 
           "Name": $scope.customerName, 
           "Address":$scope.customerAddress 
           };         
      var httpreq = { 
       method: 'POST', 
       url: 'ajs.aspx/Save', 
       headers: { 
        'Content-Type': 'application/json; charset=utf-8', 
       }, 
       data: { customerObject } 
      } 
      $http(httpreq).success(function (response) { 
       alert("Saved successfully."); 
      }) 
     }; 

另外需要注意的是,你是路过应该在的WebMethod的对象名的匹配对象名和它们各自的特性。例如

你的属性是 您相应的JSON对象必须具有相同的属性

ID:$范围。 Id

名称:$ scope。 名称

地址:$ scope。 地址