0

I'm a basic developer, I just need to collect the following JSON data in a c# file:

{
  "text":"This is a message !!!",
  "userid":"some_id",
  "username":"username",
  "fname":"some_name",
  "lname":"some_surname",
  "iurl":"url_here",
  "type":"some_type"
}

The above array is posted using angularjs.

app.js:

var app = angular.module('myApp');

app.controller('WorkingCtrl', ['$scope', '$http', function ($scope, $http) {
    $scope.form = {};
    $scope.post = [];
    $scope.submitForm = function () {
        $scope.post.text = $scope.form.text;

        if ($scope.post.text != '') {
            $scope.post.unshift($scope.post.text);           
            $scope.post.text = '';
        }

        $scope.remItem = function ($index) {
            $scope.post.splice($index , -1);
        }

        $scope.form.userid = "some_id";
        $scope.form.username = "username";
        $scope.form.fname = "some_name";
        $scope.form.lname = "some_surname";
        $scope.form.iurl = "url_here";
        $scope.form.type = "some_type";
        $scope.showValues = JSON.stringify($scope.form);
    }
}]);

As clearly seen above showValues returns the array.

Now I just wanted to collect data in my ValuesController.cs file. Can anyone suggest how to do that ?

4
  • What technology are you using? ServiceStack, Web Api, ASP.NET MVC, WCF? Commented May 21, 2015 at 7:04
  • @alexw MVC with Web Api Commented May 21, 2015 at 7:06
  • $scope.showValues = JSON.stringify($scope.form). Isn't showValues a string? See the value in debugger. You can use JavaScriptSerializer in C# method to deserialize JSON string to an object. Commented May 21, 2015 at 7:12
  • @aSharma got your point.. but some refference code is neccessary to post from angularjs to c# , what would be that ? some tutorial link would help . :) Commented May 21, 2015 at 7:16

3 Answers 3

1

You could also try this code: Create a service.

 app.service("angularService", function ($http) {
     this.AddValues= function (showValues) {
            var response = $http({
                method: "post",
                url: "api/values/YourmethodName",
                data: showValues
            });
            return response;
        }
    });

In your controller: Inject your Service here in this way to implement its method/service

app.controller('WorkingCtrl', ['$scope','angularService',function ($scope, angularService) {

$scope.submitForm = function () {
$scope.form = {};

 $scope.form.userid = "some_id";
    $scope.form.username = "username";
    $scope.form.fname = "some_name";
    $scope.form.lname = "some_surname";
    $scope.form.iurl = "url_here";
    $scope.form.type = "some_type";
    $scope.showValues = JSON.stringify($scope.form);

   angularService.AddValues(showValues ).then(function (result) {
}
}]);
Sign up to request clarification or add additional context in comments.

Comments

0

You should create a service which sends the data to your controller. You need something along these lines:

app.factory('WorkingService', ['$http', '$q', function ($http, $q) {
     saveData: function (showValues ) {
        var deferred = $q.defer();

        $http.post(yourMVCUrl, showValues ).then(function (result) {
             deferred.resolve(result);               
        });

        return deferred.promise;
    }
}

In your angular controller, just call the service WorkingService.saveData($scope.form).

1 Comment

No need for a deferred here. See: deferred anti-pattern
0

You can make call to your MVC controller (ValuesController.cs) using this code in your Angular controller:

$http({
    method: 'POST',
    url: '/Values/TestMethod',
    data: $scope.showValues,
    headers: { 'content-type': 'application/json' }
 }).
success(function (data) {
    //do some operations with the return data if the MVC controller returns data
}).
error(function () {
   alert("Error has happened..");
});

It is better to put the ajax request in a factory service and from your controller to call the http method from the service

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.