我是AngularJs和MVC的新手。当使用AngularJS和MVC在下拉列表中选择公司时,我试图获取公司的相关记录。我正在填充下拉列表,但是当选择公司时,获取相关记录的事件永远不会触发。这是我的角度控制器:
myApp.controller("CompanyController",
function($scope, $timeout, companyService)
{
getCompanies();
function getCompanies() {
companyService.getCompanies()
.success(function (data) {
$scope.companies = data;
})
.error(function (error) {
$scope.status = 'Unable to load customer data: ' + error.message;
});
};
$scope.getCurrentModules = function(){
companyId = $scope.company;
companyService.getCurrentModules(companyId)
.success(function (data){
$scope.data =data;
});
}
});
这是我的角色服务:
angular.module('dashboardManagement')
.service('companyService',[ '$ http',函数($ http){
this.getCompanies = function () {
return $http.get('/Home/GetAllCompanies');
};
this.getCurrentModules = function (id) {
return $http.get('/Home/GetCurrentModules?id=' + id);
};
}
]);
这是我的MVC视图:
<td>
Please Select an Operator:<br />
<br />
<div id="companyContainer">
<div ng-controller="CompanyController">
<select id="CompanySelector" data-ng-model="company" data-ng-change="getCurrentModules()">
<option ng-repeat="company in companies" value="{{company.CompanyID}}">{{company.vchCompanyName}}</option>
</select>
<br />
You selected: {{company}}
</div>
</div>
</td>
这是我的MVC控制器:
[System.Web.Http.HttpPost]
public JsonResult GetCurrentModules(int companyId)
{
// throw new NotImplementedException();
var modules = this._operatorDashboardRepository.GetWithStoredProcedure("scGetDashboardModulesByWidgetID @WidgetID", "companyId");
var moduleList = modules.OrderBy(module => module.vchRankingWidgetModuleHeaderText);
return Json(moduleList, JsonRequestBehavior.AllowGet);
}
谁能告诉我我做错了什么?我在MVC控制器中休息一下,看看“GetCurrentModules”是否会触发,但事实并非如此。非常感谢任何帮助。
答案 0 :(得分:0)
选项1
将getCurrentModules
方法更改为使用$http.post
而不是$http.get
。还要确保参数名称匹配
return $http.post('/Home/GetCurrentModules?companyId=' + id);
public JsonResult GetCurrentModules(int companyId)
选项2
将JsonResult
更改为允许HttpGet
[System.Web.Http.HttpGet]
public JsonResult GetCurrentModules(int companyId)
再次确保参数名称匹配