我有问题。我有这个下拉列表:
@Html.DropDownListFor(m => m.SelectCountryId, Model.Countries, @Translator.Translate("PLEASE_SELECT"), new { id = "CountryID", @class = "form-control",ng_model="countryId", ng_change = "LoadRegions(countryId);", @required = "required" })
我需要在ng_change上进入看起来像这样的MVC控制器:
[AllowAnonymous]
public JsonResult GetRegions(int countryId) // return a JsonResult
{
IUserManager manager = UserFactory.GetUserManager(WebConfiguration.DefaultTerminalId);
var model = manager.GetRegions(countryId);
return Json(model, JsonRequestBehavior.AllowGet);
}
这是角度的脚本:
$scope.LoadRegions = function (countryId)
{
console.log("COUNTRY ID: ", countryId);
$http.post('/app/Account/GetRegions/'+ countryId).then(function (response)
{
alert(response);
});
}
我获得国家/地区ID但在控制台中我收到此错误:
POST http://localhost:60789/app/Account/GetRegions/4 500(内部服务器错误)
答案 0 :(得分:1)
从它的外观来看,你的javascript中存在一些问题。
尝试以下方法。
$scope.LoadRegions = function (countryId)
{
var params = {};
params.countryId = countryId;
console.log("COUNTRY ID: ", countryId);
$http.post('/Account/GetRegions/', params).then(function (response)
{
alert(response);
});
}
正如您所看到的那样,您使用国家/地区ID传递了params对象,您也正在调用服务器端的POST - >分隔到角app
文件夹。
答案 1 :(得分:1)
MVC中的默认路由允许{controller}/{action}/{id}
,但您的控制器需要{controller}/{action}/{countryId}
。
您可以将通话更改为:
GetRegions?countryId=XXX
或将您的方法签名更改为:
public JsonResult GetRegions(int id)
或者,如果您真的想要,可以在RouteConfig.cs
修改:我刚刚意识到您正在使用$http.post
调用此功能,但代码中的所有内容都表明您希望这是GET
,所以我&#39 ; d将您的角度代码更改为$http.get()