这是我的AngularJs代码:
var adminApp = angular.module('adminApp', ['ngResource']);
adminApp.controller('adminCtrl', function ($scope, AdminService) {
AdminService.getRoles().$promise.then(function (rolesData) {
//roles data empty is coming eventhough mvc controller returns something
});
});
$scope.saveUser = function () {
var userData = { "email": $scope.useremail, "role": $scope.selectedRole.name };
AdminService.postUser({user: userData}).$promise.then(function () {
});
}
服务代码
adminApp.factory('AdminService', function ($resource) {
return $resource('', {}, {
getRoles: {
url: '/Admin/GetRoles',
method: 'GET',
isArray: true
},
postUser:{
url: '/Admin/PostUser',
method:"Post",
isArray: true,
params: {user: '@userData'}
},
});
});
发布和获取的mvc代码
public JsonResult GetRoles() {
// data count is 2 here.that means some values there.
return Json(data, JsonRequestbehavior.allowget);
}
public ActionResult PostUser(dynamic user)
{
// user empty object coming here..
return View();
}
任何人都为我提供了正确的解决方案,可以将Json发布到mvc控制器并使用$resource
概念从mvc操作中检索json。
答案 0 :(得分:0)
我已经将MVC控制器返回类型从Json修改为ContentResult。请找到以下代码
public JsonResult GetRoles() {
//data is string
return new ContentResult{ Content= data, ContentType="application/json" };
}