无法设置值来选择淘汰赛

时间:2014-07-08 07:41:42

标签: javascript model-view-controller knockout.js

我刚刚开始与Knockout(KO)合作,因此我不了解这个库的所有细节。 好的,这是我的ViewModel:

function GetDrugs(ifOther, allDrugs, doctors, id, followUpItems, foundation) {
function PatientInfo1() {
    var self = this;
    self.PatientName = ko.observable("");
    self.PatientCode = ko.observable(0);
    self.PatientAdress = ko.observable("");
    self.PatientBirthday = ko.observable("");
    self.RecivedDate = ko.observable("");
    self.AssignedTo = ko.observable("");
    self.DateIniated = ko.observable("");
    self.IfOther = ko.observable("");
    self.IsInEnrollmentQueue = ko.observable("");
    self.IsInGrandTrackerQueue = ko.observable("");
    self.FollowUpItemsName = ko.observable("");
    self.FollowupId = ko.observable("");
    self.NotificationId = ko.observable("");
    self.GrantId = ko.observable("");
    self.FollowUpItemsDate = ko.observable("");
    self.FoundationName = ko.observable("");
    self.FoundationId = ko.observable("");
    self.GrantApprovedDate = ko.observable("");
    self.GrantExpirationDate = ko.observable("");
    self.GrantAmountApproved = ko.observable("");
    self.Notes = ko.observable("");


    self.NotificationEnrollment = ko.observable("");
    self.NotificationPatient = ko.observable("");
    self.NotificationNurse = ko.observable("");
    self.DoctorsName = ko.observable("");

    self.DrugsList = ko.observable("");

    self.ifOther = ko.observable(ifOther);

    self.allDrugs = ko.observableArray(allDrugs);
    self.AllDoctors = ko.observableArray(doctors);
    self.AllFollowUpItems = ko.observable(followUpItems);
    self.AllFoundations = ko.observable(foundation);

    self.allDrugs.push("If Other");

    self.isShown = ko.computed(function () {
        return self.PatientCode() == 0;
    });

    self.ApprovePatient =
        function () {
            var dataToApprove = {
                PatientName: self.PatientName,
                PatientCode: self.PatientCode,
                PatientAdress: self.PatientAdress,
                PatientBirthday: self.PatientBirthday,
                RecivedDate: self.RecivedDate,
                AssignedTo: self.AssignedTo,
                DateIniated: self.DateIniated,
                IfOther: self.IfOther,
                IsInEnrollmentQueue: self.IsInEnrollmentQueue,
                IsInGrandTrackerQueue: self.IsInGrandTrackerQueue,
                FollowUpItemsName: self.FollowUpItemsName,
                FollowupId: self.FollowupId,
                NotificationId: self.NotificationId,
                GrantId: self.GrantId,
                FollowUpItemsDate: self.FollowUpItemsDate,
                FoundationName: self.FoundationName,
                FoundationId: self.FoundationId,
                GrantApprovedDate: self.GrantApprovedDate,
                GrantExpirationDate: self.GrantExpirationDate,
                GrantAmountApproved: self.GrantAmountApproved,
                NotificationEnrollment: self.NotificationEnrollment,
                NotificationPatient: self.NotificationPatient,
                NotificationNurse: self.NotificationNurse,
                DoctorsName: self.DoctorsName,
                DrugsList: self.DrugsList,
                Notes: self.Notes
            };
            var dataToApproveJS = ko.toJS(dataToApprove);
            $.ajax({
                url: '/api/patient',
                type: 'POST',
                data:
                    dataToApproveJS,
                contentType: 'application/x-www-form-urlencoded; charset=utf-8',
                success: function (data) {

                }
            });
        };
    self.IsInEditingMode = ko.computed(function () {
        return self.PatientCode() == 0;
    });
};
function patientViewModel(modePatient) {
    var self = this;
    self.PatientInfo = modePatient;
    $.ajax({
        url: '/api/patient/' + id,
        cache: false,
        type: 'GET',
        contentType: 'application/json; charset=utf-8',
        data: {},
        success: function (data) {
            self.PatientInfo.PatientName(data.PatientName);
            self.PatientInfo.PatientCode(data.PatientCode);
            self.PatientInfo.PatientAdress(data.PatientAdress);
            self.PatientInfo.PatientBirthday(data.PatientBirthday);
            self.PatientInfo.RecivedDate(data.RecivedDate);
            self.PatientInfo.AssignedTo(data.AssignedTo);
            self.PatientInfo.DateIniated(data.DateIniated);
            self.PatientInfo.IsInEnrollmentQueue(data.IsInEnrollmentQueue);
            self.PatientInfo.IsInGrandTrackerQueue(data.IsInGrandTrackerQueue);
            self.PatientInfo.FollowUpItemsName(data.FollowUpItemsName);
            self.PatientInfo.FollowupId(data.FollowupId);
            self.PatientInfo.NotificationId(data.NotificationId);
            self.PatientInfo.GrantId(data.GrantId);
            self.PatientInfo.FollowUpItemsDate(data.FollowUpItemsDate);
            self.PatientInfo.FoundationName(data.FoundationName);
            self.PatientInfo.FoundationId(data.FoundationId);
            self.PatientInfo.GrantApprovedDate(data.GrantApprovedDate);
            self.PatientInfo.GrantExpirationDate(data.GrantExpirationDate);
            self.PatientInfo.GrantAmountApproved(data.GrantAmountApproved);
            self.PatientInfo.NotificationEnrollment(data.NotificationEnrollment);
            self.PatientInfo.NotificationPatient(data.NotificationPatient);
            self.PatientInfo.NotificationNurse(data.NotificationNurse);
            self.PatientInfo.DoctorsName(data.DoctorsName);
            self.PatientInfo.DrugsList(data.DrugsList);
            self.PatientInfo.ifOther(data.ifOther);
            self.PatientInfo.Notes(data.Notes);
        }
    });
}
ko.applyBindings(new patientViewModel(new PatientInfo1()));

} 在我的.cshtml页面中调用此函数。 我创建了选择:

 <div class="form-group">
    <div class="col-xs-2">
        <label class="control-label">Assigned To</label>
    </div>
    <div class="col-xs-4">
        <select class="form-control" data-bind="options: AllDoctors, optionsText: 'DoctorsName', value:alert(DoctorsName), optionsCaption:''"></select>
    </div>
</div>

除了select的值没有设置外,一切正常。 在页面加载我有空选择能够从我的分配给医生选择,但我希望有之前设置的值,它存储在DoctrorsName KO变量。

1 个答案:

答案 0 :(得分:3)

您需要在绑定中绑定选定的医生。

在模型中添加选定的observable:

self.selectedDoctor = ko.observable();

然后将值绑定到observable:

<select class="form-control" data-bind="options: AllDoctors, optionsText: 'DoctorsName', value: selectedDoctor,  optionsCaption:''"></select>

然后,如果您想获得医生的姓名,您可以使用self.selectedDoctor.DoctorsName

这是一个基本的jsfiddle演示它:

http://jsfiddle.net/CZ95C/