3
我試圖使數據庫中的值出現在下拉列表中。 我使用表格成功顯示數據,但是當我添加下拉列表(<select>
)時,它停在那裏。我無法弄清楚這個問題。KnockoutJS中的下拉列表無法綁定
<table>
<thead><tr>
<th>Choice Text</th>
<th>Zero Tolerance Message</th>
<th>Has SubChoice</th>
</tr></thead>
<tbody data-bind="foreach: choice">
<tr>
<td>
<label data-bind="text: ChoiceText,visible:IsUsed"></label>
<input type="text" data-bind="value: ChoiceText, visible: !IsUsed()" >
</td>
<td>
<label data-bind="text: ZeroToleranceMessage, visible: IsUsed"></label>
<input type="text" data-bind="value: ZeroToleranceMessage, visible: !IsUsed()" />
</td>
<td>
<label data-bind="text: HasSubChoice, visible: IsUsed"></label>
<input type="text" data-bind="value: ZeroToleranceMessage, visible: !IsUsed()" />
<select data-bind="options: controlType, optionsText: 'ControlType', optionsCaption: 'CT', optionsValue: 'ControlTypeId'"/>
</td>
</tr>
</tbody>
</table>
下面是這些腳本:
<script src="~/Content/Scripts/jquery-1.9.1.js"></script>
<script src="~/Content/Scripts/jquery-1.9.1.min.js"></script>
<script src="~/Content/Scripts/knockout.js"></script>
<script src="~/Content/Scripts/knockout.mapping-latest.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(function() {
$('th > :checkbox').click(function() {
$(this).closest('table')
.find('td > :checkbox')
.attr('checked', $(this).is(' :checked'));
});
});
var viewModelChoiceJSON = ko.mapping.fromJS($.parseJSON('@Html.Raw(Model.choiceJsonData)'));
var viewModelControlTypeJSON = ko.mapping.fromJS($.parseJSON('@Html.Raw(Model.controlTypeJsonData)'));
//Html.Raw(jsonData)
ko.applyBindings({ choice: viewModelChoiceJSON });
ko.applyBindings({ controlType: viewModelControlTypeJSON });
});
</script>
控制器:
public ActionResult ChoiceList(int? questionId)
{
_ValidationService = DiFactory.Resolve<IValidationService>();
_ChoiceService = DiFactory.Resolve<IChoiceService>();
ChoiceViewModel viewModel = new ChoiceViewModel(_ChoiceService.GetChoice(questionId));
viewModel.choiceJsonData = JsonConvert.SerializeObject(_ChoiceService.GetChoice(questionId));
viewModel.controlTypeJsonData = JsonConvert.SerializeObject(_ValidationService.GetControlType());
// viewModel.ControlTypeSource = Utility.ControlTypeSource();
return PartialView("~/Areas/Validation/Views/Choice/ChoiceGrid.cshtml", viewModel);
}
什麼是'$ .parseJSON('@ Html.Raw(模型的價值。 controlTypeJsonData)')'和之前的那個?你的下拉代碼在哪裏? – FakeRainBrigand
@FakeRainBrigand:這裏是下拉代碼'
@FakeRainBrigand @Html。 Raw(Model.controlTypeJsonData)返回
'[{「ControlTypeId」:1,「ControlType」:「Textbox」},{「ControlTypeId」:2,「ControlType」:「CheckBox」},{「ControlTypeId」:3,「當我顯示$ .parseJSON('@ Html.Raw(Model.controlTypeJsonData)')時,「ControlType」:「RadioButton(Yes/No)」},{「ControlTypeId」:4,「ControlType」:「DropDownList」}]一個消息框(警告)它返回[對象對象],[對象對象],[對象對象] – Allen