【发布时间】:2015-01-12 21:39:48
【问题描述】:
这是我第一次使用 Knockout.js。
我不明白为什么我会遇到这个问题。
未捕获的错误:无法解析绑定。 消息:ReferenceError:DataPrevista 未定义; 绑定值:value:DataPrevista,namePath:true
我希望有人帮助我。
这是我的代码:
查看
<div>
@Html.LabelFor(model => model.Cliente)
</div>
<div>
@Html.DropDownListFor(model => model.Cliente.Codigo, new SelectList(Model.ClientesSelecionaveis, "Value", "Text", Model.Cliente), new { @class = "form-control" })
</div>
<div>
@Html.LabelFor(model => model.Funcionario)
</div>
<div>
@Html.DropDownListFor(model => model.Funcionario.Codigo, new SelectList(Model.FuncionariosSelecionaveis, "Value", "Text", Model.Cliente), new { @class = "form-control" })
</div>
<fieldset>
<div style="margin:30px 0;">
<input type="button" value="Adicionar Agendamento" data-bind="click: addAgendamento" class="btn btn-success" />
<input type="button" value="Remover Agendamento" data-bind="click: removeAgendamento" class="btn btn-danger" />
</div>
<h4 class="page-header">Agendamentos</h4>
@Html.EditorFor(model => model.Agendamentos)
</fieldset>
<p style="margin:30px 0;">
<input type="submit" value="Enviar" class="btn btn-info" />
@Html.ActionLink("Listar", "Index")
</p>
编辑器模板
<div>
@Html.LabelFor(model => model.DataPrevista)
</div>
<div>
@Html.TextBoxFor(model => model.DataPrevista, new { @class = "form-control", data_bind = "value: dataPrevista, namePath: true" })
</div>
<div>
@Html.LabelFor(model => model.InicioPrevisto)
</div>
<div>
@Html.TextBoxFor(model => model.InicioPrevisto, new { @class = "form-control", data_bind = "value: inicioPrevisto, namePath: true" })
</div>
<div>
@Html.LabelFor(model => model.FimPrevisto)
</div>
<div>
@Html.TextBoxFor(model => model.FimPrevisto, new { @class = "form-control", data_bind = "value: fimPrevisto, namePath: true" })
</div>
<div>
@Html.LabelFor(model => model.TrasladoPrevisto)
</div>
<div>
@ Html.TextBoxFor(model => model.TrasladoPrevisto, new { @class = "form-control", data_bind = "value: trasladoPrevisto, namePath: true" })
Javascript
<script type="text/javascript" language="javascript">
function createViewModel() {
var createAgendamento = function () {
return {
dataPrevista: ko.observable(),
inicioPrevisto: ko.observable(),
fimPrevisto: ko.observable(),
trasladoPrevisto: ko.observable()
};
};
var addAgendamento = function () {
agendamentos.push(createAgendamento());
};
var removeAgendamento = function () {
agendamentos.pop();
};
var cliente = ko.observable();
var funcionario = ko.observable();
var agendamentos = ko.observableArray([createAgendamento()]);
return {
cliente: cliente,
funcionario: funcionario,
agendamentos: agendamentos,
addAgendamento: addAgendamento,
removeAgendamento: removeAgendamento
};
}
$(document).ready(function () {
var viewModel = createViewModel();
ko.applyBindings(viewModel);
});
【问题讨论】:
-
错误信息非常清楚:“DataPrevista 未定义”。所以在你的 html 中的某个地方你有一个绑定
data-bind="value:DataPrevista"但你的 javascript viewmodel 没有这个属性。但是在您问题的代码中有DataPrevista,所以您发布了错误的代码......所以没有人会告诉您如何解决这个问题。 -
对不起,我贴错了代码。
标签: c# asp.net-mvc knockout.js