我有一个带有字符串属性的敲门模型,一个int属性和两个其他模型的列表。在视图中,我试图验证knockout对象中的属性,此时只是确保它们存在。
我正在使用使用extends的Knockout.Validation库。
我遇到的问题是,当我点击按钮发送验证请求时,有一个javascript错误 self.isValid不是函数
我已经在网上查了一下,似乎其他人有问题,只有他们所拥有的解决方案对我没用。
Javascript模型
EditTestStep
var StringParameterViewModel = function(data) {
var self = this;
if (data != null) {
ko.mapping.fromJS(data, stringParameterMapping, self);
} else {
self.ParameterName = ko.observable().extend({
required: {message: 'Please enter a parameter name.'}
});
self.StringValue = ko.observable().extend({
required: { message: 'A value for the parameter is needed.' }
});
}
}
var XmlParameterViewModel = function (data) {
var self = this;
if (data != null) {
ko.mapping.fromJS(data, xmlParameterMapping, self);
} else {
self.ParameterName = ko.observable().extend({
required: true
});
self.XmlValue = ko.observable().extend({
required: true
});
}
}
var xmlParameterMapping = {
craete: function(options) {
return XmlParameterViewModel(options.data);
}
}
var stringParameterMapping = {
create: function(options) {
return StringParameterViewModel(options.data);
}
}
var editTestStepMapping = {
create: function(options) {
return EditTestStepViewModel(options.data);
}
}
var EditTestStepViewModel = function(data) {
var self = this;
if (data != null) {
ko.mapping.fromJS(data, {}, self);
} else {
self.StringParameters = ko.observableArray();
self.XmlParameters = ko.observableArray();
self.TestStepName = ko.observable().extend({
required: true
});
}
self.saveTestStep = function() {
var dataToSend = ko.mapping.toJSON(self);
$.ajax({
url: "/Home/SaveEdit/",
type: "POST",
contentType: "application/json",
data: dataToSend
});
};
self.Errors = ko.validation.group(self);
self.checkValid = function () {
if (self.isValid()) {
alert('All ok!');
} else {
self.Errors.showAllMessages();
}
}
}
var validationOptions = {
insertMessages: true,
decorateElement: true,
errorElementClass: 'errorCSS',
messagesOnModified: true,
debug: true,
grouping: {
deep: true,
observable: false //Needed so added objects AFTER the initial setup get included
}
};
ko.validation.init(validationOptions, true);
查看
@using System.Web.Script.Serialization
@model MvcNewPatternsDemo.Models.EditTestStepViewModel
@{ string data = new JavaScriptSerializer().Serialize(Model);}
@{
ViewBag.Title = "Home Page";
}
@section scripts{
<script src="~/Scripts/knockout-3.4.0.js"></script>
<script src="~/Scripts/knockout.validation.js"></script>
<script src="~/Scripts/knockout.mapping-latest.js"></script>
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/EditTestStep.js"></script>
<script type="text/javascript">
var editTestStepViewModel = new EditTestStepViewModel(@Html.Raw(data));
ko.applyBindingsWithValidation(editTestStepViewModel);
</script>
}
<form>
<input class="form-control" name="ParameterName" data-bind="value: TestStepName"/>
<input class="form-control" name="TestStepId" data-bind="value: TestStepId"/>
<table class="table table-striped">
<tr>
<th>StringParameters</th>
</tr>
<tbody data-bind="foreach: StringParameters">
<tr>
<td class="form-group"> <input name="ParameterName" class="form-control input-sm" data-bind="value: ParameterName"/></td>
<td class="form-group"> <input name="StringValue" class="form-control input-sm" data-bind="value: StringValue"/></td>
</tr>
</tbody>
</table>
<table class="table table-striped">
<tr>
<th>XmlPara</th>
</tr>
<tbody data-bind="foreach: XmlParameters">
<tr>
<td class="form-group"> <input name="ParameterName" class="form-control input-sm" data-bind="value: ParameterName" /></td>
<td class="form-group"> <input name="XmlValue" class="form-control input-sm" data-bind="value: XmlValue" /></td>
</tr>
</tbody>
</table>
</form>
<div class="row">
<button data-bind="click: saveTestStep" type="submit">Save Test Step</button>
<button data-bind="click: checkValid"type="button">Check Valid</button>
</div>
模型
namespace MvcNewPatternsDemo.Models
{
public class TestStepDisplayModel
{
public int TestStepId { get; set; }
public string TestStepName { get; set; }
}
public class StringParameterViewModel
{
public string ParameterName { get; set; }
public string StringValue { get; set; }
}
public class XmlParameterViewModel
{
public string ParameterName { get; set; }
public string XmlValue { get; set; }
}
[Serializable]
public class EditTestStepViewModel
{
public string TestStepName { get; set; }
public int TestStepId { get; set; }
public List<StringParameterViewModel> StringParameters { get; set; }
public List<XmlParameterViewModel> XmlParameters { get; set; }
public EditTestStepViewModel()
{
this.StringParameters = new List<StringParameterViewModel>();
this.XmlParameters = new List<XmlParameterViewModel>();
}
}
}
对控制器的ajax调用工作,并且正在发送来自对象的所有数据,我只需要验证即可运行。
答案 0 :(得分:1)
我在您的javascript中看不到您注册扩展程序的行。
尝试在最后添加:
ko.validation.registerExtenders();