Hello

I'm trying to tie a dropdownlist to my object. I take a list of items through the lstTypes variable. When I use "track by" - then for some reason the last element is always selected and the choice cannot be changed in the dropdownlist:

<select ng-model="Project.TypeID" id="Project.TypeID" ng-options="type.Value as type.Text for type in Project.lstTypes track by Project.TypeID"> 

My object in MVC:

 namespace MyProject.ModelView { public class ProjectModelView { public int ID { get; set; } public string NameEng{ get; set; } public string Type { get; set; } public int TypeID { get; set; } public List<ProjectType> _ProjectTypes; public IEnumerable<SelectListItem> lstTypes { get { _ProjectTypes = (from u in dal.DBProjectTypes select u).ToList<ProjectType>(); return new SelectList(_ProjectTypes, "ID", "value"); } } } } 

 <script> var app = angular.module('Proj', ['ngRoute']) app.config(function ($routeProvider, $locationProvider) { $locationProvider.html5Mode(true) $routeProvider .when('/Project/PEdit/:someId', { controller: 'projectDisplay' , templateUrl: 'tmpl-view-project' }) }) app.controller('projectDisplay', function ($scope, $http, $routeParams) { $scope.route = {id:-1} if ($routeParams.someId != undefined) { $scope.route = { id: $routeParams.someId } console.log("1") } console.log($scope.route.id) if ($scope.route.id != -1) { console.log("3") $http({ url: '/Project/GetProjectsByIDJSON', params: { pID: $scope.route.id }, method: 'get' }).then(function (data) { $scope.Project = data.data; $scope.Project.TypeID = parseInt($scope.Project.TypeID) }) } $scope.Add = function () { $http({ method: "POST", data: $scope.Project.myProj, url: "/Project/Submit" }). success(function (data, status, headers, config) { }) } }) </script> И в конце файл CSHTML: 
 <div ng-app="Proj" ng-controller="projectDisplay"> <div ng-view></div> <script type="text/ng-template" id="tmpl-view-project"> <div class="panel-group" id="accordion"> <!-- accordion 1 --> <div class="panel panel-primary"> <div class="panel-heading"> <!-- panel-heading --> <h4 class="panel-title" role="button" data-toggle="collapse" data-parent="#accordion" href="#accordionOne" aria-expanded="false" aria-controls="collapseOne"> <!-- title 1 --> <a> Project Details </a> </h4> </div> <!-- panel body --> <div id="accordionOne" class="panel-collapse collapse in"> <!-- Project content --> <div class="top-content"> <div class="inner-bg"> <div class="container"> <div class="col-sm-6 form-box"> <div> <div class="form-group"> <label for="form-last-name">@Html.LabelFor(m => m.NameEng)</label> <input id="Project.NameEng" ng-model="Project.NameEng" name="Project.NameEng" type="text" class="form-first-name form-control" placeholder="Project name in english..." /> </div> <div class="form-group"> <select ng-model="Project.TypeID" id="Project.TypeID" ng-options="type.Value as type.Text for type in Project.lstTypes"> <option value="">-- Choose Type --</option> </select> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> 

  • When I use "track by" - then for some reason the last element is always selected and the choice cannot be changed in the dropdownlist - does this work for sure? and there are no errors in the console? - Grundy

0