我有这个AngularJS代码来获取国家
function helperController($scope, $http) {
console.log("entering the controller");
var baseUrl = "http://localhost:8000/api";
//schools
var getSchools = function (result) {
console.log("getting schools");
if (result != []) {
$scope.schools = result;
} else {
$scope.schools = null;
}
};
var onError = function (reason) {
console.log(reason);
$scope.error = "No schools seeding information";
};
console.log($scope.schoolId);
$http({
url: baseUrl + "/schools",
type: "GET",
contentType: "application/json",
})
.success(function (result) {
console.log(result);
getSchools(result);
}).error(function (reason) {
onError(reason);
});
//getting states
//get the country where country name is nigeria
$http({
url: baseUrl + "/countries",
type: "GET",
contentType: "application/json",
params: { countryId: 160 },
}).success(function (result) {
$scope.NigeriaID = result.NigeriaID;
}).error(function (reason) {
console.log("cannot get Nigeria ID ");
});
var getStates = function (result) {
console.log(result);
$scope.states = result;
};
var errorGettingStates = function (reason) {
console.log("cannot get states" + reason);
};
$http({
url: baseUrl + "/states",
type: "GET",
contentType: "application/json",
params: { id: $scope.NigeriaID }
}).success(function (result) {
console.log("getting countries");
getStates(result);
}).error(function (reason) {
errorGettingStates(reason);
});
}
此代码正确地从托管的api获取学校和国家。
这是我的.chtml页面
<div class="form-group">
@Html.LabelFor(model => model.SchoolID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*<select data-ng-model="selectedState" class="form-control">
<option data-ng-repeat="school in schools" value="{{school.id}}">{{school.name}}</option>
</select>*@
@Html.DropDownListFor(model => model.SchoolID,new SelectList("select school"),"Select School",
new
{
@class = "form-control",
@ng_model = "schoolId",
@ng_options = "s.id as s.name for s in schools",
})
@Html.ValidationMessageFor(model => model.SchoolID, "", new { @class = "text-danger" })
<label id="lab1">{{SchoolId}}</label>
</div>
</div>
表单中的第一个值是&#34;选择学校&#34;并从数据库中填充其他值。 当我选择下一个值时,例如,如果下拉列表包含这样的值 如
如果我选择&#34; Biochem Lab School&#34;并提交表格,下拉列表的值应该为1,但是得到0。
请帮助,我做错了什么?
答案 0 :(得分:0)
<select data-ng-model="person.MaritalStatus" name="MaritalStatus" >
<option value="9999">-- Select Marital Status --</option>
<option ng-repeat="c in MaritalStatu" value="{{c.MaritalStatus}}">{{c.MaritalStatus}}</option>
</select>
尝试以上操作,替换自己的变量