arrays - Returning object from webapi to angularjs - not defined error -
model:
public class examplemodel { public int64 id { get; set; } public string name { get; set; } }
apicontroller:
[httppost] public ihttpactionresult action(examplemodel model) { if (!modelstate.isvalid) { return badrequest(modelstate); } if (modelstate.isvalid) { //do return ok(model); } return badrequest(); }
angularjs controller:
app.controller('createcontroller', ['$scope', '$http', function ($scope, $http) { $scope.example = { name: "" }; $scope.create = function () { $http.post('api/create', this.example).success(function (response) { $scope.examples.push(response); }) .error(function (response) { var errors = []; (var key in response.data.modelstate) { (var = 0; < response.data.modelstate[key].length; i++) { errors.push(response.data.modelstate[key][i]); } } $scope.errors = errors; }); }; }]);
when try push 'example' object 'examples' array error saying "the response not defined".
but when try extract individual elements response , store variable works. example:
$http.post('api/create', this.example).success(function (response) { var example = {}; example.id = reponse.id; example.name = response.name; $scope.examples.push(example); //this works })
so, why reponse server not defined? how can solved without creating variable object?
Comments
Post a Comment