How to read observable array in Knockout js to read generic collection list
Here i am not albe to get details how to read observable array in Knockout to read generic collection list.
i have to store this list in new array with id and name.
any help will be highly appriciated.
public class StudentDetail : IStudentDetail {
public StudentDetail() { }
public List<StudentInfo> GetCountryCode() {
var _studentInfo = new List<StudentInfo>();
_studentInfo.Add(new StudentInfo { ID = "1", Code = "123", Name = "ABC" });
_studentInfo.Add(new StudentInfo { ID = "2", Code = "124", Name = "XYZ" });
_studentInfo.Add(new StudentInfo { ID = "3", Code = "125", Name = "PQR" });
_studentInfo.Add(new StudentInfo { ID = "4", Code = "126", Name = "MNO" });
return _studentInfo;
}
}
public interface IStudentDetail {
List<StudentInfo> GetStudentDetail();
}
In controller Class :
//studendetails is list. after injecting dependency i am able to fetch values here.
var vm= new viewmodelStudentDetails{
studendetails = GetStudentDetail();
return( vm);
}
In CSHTML page
@Model xyz.Student.viewmodelStudentDetails
<script type="text/javascript">
var data = {
studentdetail:"@Model.studendetails"
};
studentdetails.initialize(args);
ko.applyBindings(studentdetails);
</script>
}
external JS knockout binding
studentdetails = (function () {
_studentdetais = ko.observableArray(),
var initialize = function (data) {
_studentdetais(data.studentdetail);
};
return studentdetails :_studentdetais
};
}()));