Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJs下拉列表值清除模型值_Angularjs_Html Select - Fatal编程技术网

AngularJs下拉列表值清除模型值

AngularJs下拉列表值清除模型值,angularjs,html-select,Angularjs,Html Select,我的下拉式html如下所示: <select name="originType" class="form-control" ng-model="myLocationType" ng-options="type.Key as type.Value for type in allLocationTypes" required></select> 这可以很好地填充下拉列表。然而,当我加载页面并用模型数据填充它时,应该选择的值

我的下拉式html如下所示:

<select name="originType" class="form-control"
        ng-model="myLocationType"
        ng-options="type.Key as type.Value for type in allLocationTypes"
        required></select>

这可以很好地填充下拉列表。然而,当我加载页面并用模型数据填充它时,应该选择的值永远不会被忽略。下拉列表始终为默认(空白)值。我对不是键/值对的下拉列表没有问题。例如:

<select name="shipId" class="form-control"
        ng-model="WageAgreement.ShipId"
        ng-options="shipId for shipId in manufacturers"
        required></select>

如果模型值与数组中提供的值匹配,则填充模型值应该有效。请参见其中,当设置为WageAgreement.ShipId的模型值时,选择了2

var mod = angular.module("myApp", []);

mod.controller("MainController", function ($scope) {
    $scope.allLocationTypes = [
        {Key: 1, Value: "Shipyard 1"},
        {Key: 2, Value: "Shipyard 2"},
        {Key: 3, Value: "Shipyard 3"}
    ];
    $scope.WageAgreement = {};

    //Populate value here
    $scope.WageAgreement.ShipId = 2;
});
模型值是否与对象数组中的键匹配?如果没有选择下拉列表中的值,我怀疑某些内容不匹配


如果这还不能回答您的问题,您能否用JSFIDLE或您如何填充制造商/allLocations和WageAgreement的示例更新您的帖子?

Angular始终在select中提供默认选项的设置。当模型未定义时显示。如果您希望默认选项是其他选项,则为选择模型指定一个值。您是对的。关键是C#enum。使用[JsonConverter(typeof(StringEnumConverter))]返回模型,这导致枚举作为与枚举类似的字符串返回。下拉列表是通过直接获取枚举来填充的,这意味着该值存储为整数。更改为使用枚举的字符串值,一切正常!