AngularJS&asp.net MVC-将数据传递到控制器? [英] AngularJS & asp.net MVC - passing data to controller?
问题描述
在此示例中 http://xombe.com/2014/02/17/combine-angular-js-with-asp-net-mvc/,如何将数据传递到asp.net MVC控制器?在示例中,他没有这样做.他访问控制器并创建一个新对象,该对象被传递回视图.都是只读的.
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.
这是我的Angular代码:
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>
按钮中触发了"update()":
The "update()" triggered in a button:
<input type="submit" ng-click="updatefromview()" value="update"/>
单击按钮时,我进入了asp.net MVC控制器,但没有任何传入.asp.net MVC控制器签名如下:
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 始终为null.我希望看到文本"testData".我在做什么错了?
somedata is always null. I'm expecting to see the text "testData". What am I doing wrong?
推荐答案
尝试将 new {somedata ="testData"}
中的第三个参数"testData"
替换为 Url.Action()
方法.
Try replacing third argument "testData"
with new{ somedata = "testData" }
in Url.Action()
method.
第三个参数是 RouteValueDictionary ,它是类似于键值对的东西,因此您需要使用通过 new {}
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 { }
这篇关于AngularJS&asp.net MVC-将数据传递到控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!