我有一个要使用dropdownlist
和MVC
绑定angular JS
的位置。
我尝试如下
var app1 = angular.module('Assign', [])
app1.controller('SAPExecutive_R4GState', function ($scope, $http, $window) {
// alert(UMSLocationDetails);
var LocObj = JSON.parse(UMSLocationDetails)
var ZoneValue = "";
$.each(LocObj, function (index, element) {
ZoneValue += element.LocationID + ",";
});
ZoneValue = ZoneValue.substr(0, ZoneValue.length - 1);
var Values = { "MaintZones": ZoneValue };
alert(JSON.stringify(Values));
$scope.DefaultLabel = "Loading.....";
$http({
method: "POST",
url: "/App/GetR4GStates",
dataType: 'json',
data: JSON.stringify(Values),
headers: { "Content-Type": "application/json" }
}).success(function (result) {
$scope.DefaultLabel = "--Select--";
$scope.State = data;
});
post.error(function (data, status) {
$window.alert(data.Message);
});
});
<select id="SAPExecutive_R4GState" class="form-control" ng-model="R4GState.Selected" ng-controller="SAPExecutive_R4GState as R4GState" ng-init="Select" ng-options="b for b in list">
</select>
还有我的CS代码
[HttpPost]
public JsonResult GetR4GStates(string MaintZones)
{
List<SelectListItem> lstR4GState = new List<SelectListItem>();
try
{
Filters ObjFilter = new Filters();
DataTable dt = ObjFilter.GetR4GState(MaintZones);
if (dt.Rows.Count > 0)
{
lstR4GState = (from DataRow dr in dt.Rows
select new SelectListItem()
{
Text = Convert.ToString(dr["R4GSTATENAME"]),
Value = Convert.ToString(dr["R4GSTATECODE"])
}).ToList();
}
else
{
SelectListItem slEmptyData = new SelectListItem();
slEmptyData.Text = "No Data found";
slEmptyData.Value = "No Data found";
lstR4GState.Add(slEmptyData);
}
}
catch (Exception e)
{
}
// ErrorLog.HandleErrorLog("", "", "GetR4GStates", "Error2");
return Json(lstR4GState);
}
我正在return Json(lstR4GState);
中获取值,但下拉菜单中未设置这些值。
由于我是棱角新手,请提出我要去哪里的问题
答案 0 :(得分:1)
建议THEN
中的用户CATCH
和AngularJS
> 1.5
这应该起作用:
angular.module('Assign', [])
.controller('SAPExecutive_R4GState', function($scope, $http, $window) {
const payload = {};
$http.get('https://dog.ceo/api/breeds/list/all', payload)
.then(function(response) {
// Please be aware RESPONSE contains the whole response.
// You probably want RESPONSE.DATA
console.log(response.data);
})
.catch(function(err) {
// do something with err message });
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="Assign" ng-controller="SAPExecutive_R4GState"></div>
请注意,此示例使用了GET
请求,只是为了让我可以证明它可行,
在GET
请求中,payload
将被添加为querystring
如果将其更改为$http.post
,则有效负载将添加到请求的正文中。
答案 1 :(得分:0)