1

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?

1
  • CodeProject always misses the mark by posting convoluted examples. Commented Nov 29, 2014 at 18:26

1 Answer 1

2

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 { }

Sign up to request clarification or add additional context in comments.

3 Comments

Welcome. Can you check just one thing be renaming somdata variable to some other name and check that value posts or not
No - the value does not come through if the variable is renamed. The javascript and .net parameters need to match.
That was the thing I needed to tell you so I asked you to try yourself

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.