我的select元素的foreach绑定遇到了一些麻烦。我以前工作过,不知道为什么它停止工作,尝试了很多不同的方法似乎没什么用。
这些是我的js文件中的相关项目:
Datamodels js file:
function ProviderModel(data) {
var self = this;
var defaultValue = '';
var firstName = defaultValue;
var lastName = defaultValue;
var salutation = defaultValue;
if (data != undefined) {
firstName = data.FirstName;
lastName = data.LastName;
salutation = data.Salutation;
}
self.FirstName = ko.observable(firstName);
self.LastName = ko.observable(lastName);
self.Salutation = ko.observable(salutation);
self.formattedFullName = ko.computed(function () {
return self.LastName() + ", " + self.FirstName() + " " + self.Salutation();
});
}
function PracticeModel(data) {
var self = this;
var defaultValue = '';
var street = defaultValue;
var street2 = defaultValue;
var city = defaultValue;
var state = defaultValue;
var postalCode = defaultValue;
var phone = defaultValue;
var taxId = defaultValue;
if (data != undefined) {
street = data.Street;
street2 = data.Street2;
city = data.City;
state = data.State;
postalCode = data.PostalCode;
phone = data.Phone;
taxId = data.TaxId;
}
self.Street = ko.observable(street);
self.Street2 = ko.observable(street2);
self.City = ko.observable(city);
self.State = ko.observable(state);
self.PostalCode = ko.observable(postalCode);
self.Phone = ko.observable(phone);
self.TaxId = ko.observable(taxId);
self.displayPhone = ko.computed(function () {
return "Phone #: " + format10DigitPhoneNumberByText(self.Phone());
});
self.displayTaxId = ko.computed(function () {
return "Tax ID: " + self.TaxId();
});
self.formattedFullAddress = ko.computed(function () {
return self.Street() + " " +
self.Street2() + " " +
self.City() + " " +
self.State() + " " +
self.PostalCode();
});
}
function ContactModel(data) {
var self = this;
var defaultValue = '';
var firstName = defaultValue;
var lastName = defaultValue;
var contactType = 0;
var otherContactTitle = defaultValue;
var contactTitles = [];
if (data != undefined) {
firstName = data.FirstName;
lastName = data.LastName;
contactTitles = data.Titles;
contactType = data.ContactType;
otherContactTitle = data.OtherContactTitle;
}
self.FirstName = ko.observable(firstName);
self.LastName = ko.observable(lastName);
self.Titles = ko.observableArray(contactTitles);
self.ContactType = ko.observable(contactType);
self.OtherContactTitle = ko.observable(otherContactTitle);
self.BoxStyle = ko.computed(function () {
if (self.FirstName() != "" && self.LastName() != "") {
return "contactBoxSelected";
}
return "contactBoxUnselected";
});
}
这是ViewModel js文件:
function ProviderDetailsViewModel() {
var self = this;
self.Provider = ko.observable(new ProviderModel());
self.Practice = ko.observable(new PracticeModel());
self.Contact = ko.observable(new ContactModel());
self.DisplayOtherContactTextBox = ko.observable(false);
self.DataPopulated = ko.observable(false);
//subscriptions
self.Contact().ContactType.subscribe(function (newValue) {
var result = Enumerable.From(self.Contact().Titles()).First(function (x) {
if (x.Text() == "Other") { return x; }
else { return null; }
}).Value();
if (result === newValue) {
self.DisplayOtherContactTextBox(true);
} else {
self.Contact().OtherContactTitle('');
self.DisplayOtherContactTextBox(false);
}
}, self);
} //ProviderDetailsViewModel End
这是我的单页应用程序js文件,我在其中应用我的绑定并执行页面加载
var spa = {
viewModels: {
providerDetails: new ProviderDetailsViewModel(),
patientSearch: new PatientSearchViewModel()
}
};
$(document).ready(function () {
$.getJSON('/Cardiology/NewRequest/ProviderDetails', function (response) {
ko.mapping.fromJS(response, {}, spa.viewModels.providerDetails);
spa.viewModels.providerDetails.DataPopulated(true);
});
ko.applyBindings(spa.viewModels.providerDetails);
});
示例json从我的asp.net MVC控制器返回:
{
"Provider":
{
"FirstName":"Bat",
"LastName":"Man",
"Salutation":"MD"
},
"Practice":
{
"Street":"19999 Batcave",
"Street2":null,
"City":"Cincinnati",
"State":"OH",
"PostalCode":"98122",
"Phone":"9999999999",
"TaxId":"45-77777777"
},
"Contact":
{
"FirstName":"Bat",
"LastName":"Man",
"Titles":
[
{"Selected":false,"Text":null,"Value":null},
{"Selected":false,"Text":"RN","Value":"725060000"},
{"Selected":false,"Text":"MD","Value":"725060001"},
{"Selected":false,"Text":"Physician's Assistant","Value":"725060002"},
{"Selected":false,"Text":"Medical Assistant","Value":"725060003"},
{"Selected":false,"Text":"Pharmacist","Value":"725060004"},
{"Selected":false,"Text":"Office Staff","Value":"725060005"},
{"Selected":false,"Text":"Office Manager","Value":"725060006"},
{"Selected":false,"Text":"Billing Personnel","Value":"725060007"},
{"Selected":false,"Text":"Finance Personnel","Value":"725060008"},
{"Selected":false,"Text":"Claims Personnel","Value":"725060009"},
{"Selected":false,"Text":"Receptionist","Value":"725060010"},
{"Selected":false,"Text":"Other","Value":"725060011"}
]}
}
这是我的标记:
<section id="ProviderDetails" data-bind="with: spa.viewModels.providerDetails">
<table class="newRequestInnerTable2" width="100%" cellpadding="5" data-bind="visible: DataPopulated">
<tr>
<td width="120px" align="left">*Provider:</td>
<td>
<div id="selectedProvider">
<table width=100% height="75">
<tr>
<td>
<div class="searchSelectedBox">
<span data-bind="text: Provider().formattedFullName"></span><br />
<span style="font-size: 12px;" data-bind="text: Practice().formattedFullAddress"></span><br />
<span textType="phoneNumber10" data-bind="text: Practice().displayPhone"></span><br />
<span style="font-size: 12px;" data-bind="text: Practice().displayTaxId"></span>
</div>
</td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td width="120px" align="left">*Contact:</td>
<td data-bind="with: Contact">
<div id="contactInfo" data-bind="css: BoxStyle">
<table width="100%">
<tr>
<td class="tdl">Contact First Name: </td>
<td class="tdr">
<input id="ContactFirstName" type="text" data-bind="value: FirstName"/>
</td>
<td class="tdl">Contact Last Name: </td>
<td class="tdr">
<input id="ContactLastName" type="text" data-bind="value: LastName"/>
</td>
</tr>
<tr>
<td class="tdl">Title/Position: </td>
<td colspan="3" class="tdr">
<select data-bind="foreach: Titles, value: ContactType">
<option data-bind="value: Value, text: Text"></option>
</select>
<input id="otherContactTitle" type="text" data-bind="visible: $parent.DisplayOtherContactTextBox, value: OtherContactTitle"/>
</td>
</tr>
</table>
@*<p data-bind="text: ko.toJSON($data.Titles, null, 2)"></p>
<pre data-bind="text: ko.toJSON($data, null, 2)"></pre>*@
</div>
</td>
</table>
</section>
<style type="text/css">
.tdl {text-align: right}
.tdr { text-align: left;padding-left: 4px;}
.tdr input { width: 175px;}
</style>
我有一个非常简单的例子,我使用这些数据并且它正在工作,我无法弄清楚这里缺少什么。我真的需要一个&#34;第二个&#34;这双眼睛。
当我使用firebug on&#39; spa.viewModels.providerDetails.Contact()。标题()&#39;它充分填充了适当的数据。页面上的所有其他数据都在显示,所以我知道我已连接
非常感谢。
答案 0 :(得分:0)
感谢Jason Haley指出我使用错误绑定的那一刻。
答案是:
我会用选项替换foreach:标题, optionsValue:'Value',optionsText:'Text'。看一下这个例子3 page:knockoutjs.com/documentation/options-binding.html - Jason Haley