4thSpace
4thSpace

Reputation: 44312

AngularJS & asp.net MVC - passing data to controller?

From this example http://xombe.com/2014/02/17/combine-angular-js-with-asp-net-mvc/, how do I pass data to the asp.net MVC controller? In the example, he doesn't do this. He accesses the controller and creates a new object, which is passed back to the view. It's all read only.

Here's my Angular code:

    <script type="text/javascript">
        var myApp = angular.module('myApp', [])
        .controller('myAppCtrl', function ($scope, $http) {
            $scope.updatefromview = function () {
                $http.post("@Url.Action("UpdateSomething", "Home", "testData")").success (function(data) {
                    $scope.somestring = data;
                });
            };
        });
    </script>

The "update()" triggered in a button:

<input type="submit" ng-click="updatefromview()" value="update"/>

When the button is clicked, I go into the asp.net MVC controller but nothing is passed in. The asp.net MVC controller signature looks like this:

    [HttpPost]
    public ActionResult UpdateSomething(string somedata)

somedata is always null. I'm expecting to see the text "testData". What am I doing wrong?

Upvotes: 1

Views: 1431

Answers (1)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

Try replacing third argument "testData" with new{ somedata = "testData" } in Url.Action() method.

The third parameter is RouteValueDictionary which is key value pair like thing so you need to specify it using anonymous type which is made by using new { }

Upvotes: 2

Related Questions