我正在使用asp.net MVC Core构建应用程序。我使用Knockout允许用户动态地在应用程序表单中添加/删除fieldset
。该页面将呈现"成功页面的部分视图"如果表格已成功提交。
但是,如果有任何字段未通过验证,我就不知道为什么要删除整个fieldset
。以下是截图:
我在表单中使用的脚本:
@section Scripts
{
<script type="text/javascript" src="~/lib/knockout/dist/knockout.js"></script>
<script type="text/javascript" src="~/lib/knockout.namepathbinding.js"></script>
<script type="text/javascript">
function ViewModel() {
var createReferee = function () {
return {
refereeName: ko.observable(),
refereePhone: ko.observable(),
refereeEmail: ko.observable(),
refereePosition: ko.observable(),
refereeCompanyName: ko.observable()
};
};
var addReferee = function() {
referees.push(createReferee());
};
var removeReferee = function() {
referees.pop();
};
var referees = ko.observableArray([createReferee()]);
return {
referees: referees,
addReferee: addReferee,
removeReferee: removeReferee
};
}
$(document).ready(function () {
var viewModel = ViewModel();
ko.applyBindings(viewModel);
});
$(document).on('submit', '#applyForm', function () {
var $form = $(this);
$.post('', $form.serialize())
.done(function (response) {
$form.replaceWith(response);
})
return false;
});
</script>
}