我在mvc中工作。我有一个视图,其中使用 ajax post、knockout、javascript 将表单发布到服务器。表单中的名称字段是必需的,代码字段是必需的。因此,我在表格中使用了以下代码作为名称:-
<input type="text" data-bind="value:Name" placeholder = "Enter Name" required /><br />
我使用以下javascript发布表格
<script type="text/javascript">
var EmpViewModel = function () {
//Make the self as 'this' reference
var self = this;
//Declare observable which will be bind with UI
self.Code = ko.observable("");
self.Name = ko.observable("");
self.DateOfBirth = ko.observable("");
self.Age = ko.observable("");
self.ContactNumber = ko.observable("");
self.Email = ko.observable("");
self.Address = ko.observable("") ;
self.MaritalStatus = ko.observable("");
self.City = ko.observable("");
self.Reference = ko.observable("");
//The Object which stored data entered in the observables
var EmpData = {
EmpCode: self.Code,
EmpName: self.Name,
Dob: self.DateOfBirth,
Age: self.Age,
ContactNumber: self.ContactNumber,
MaritalStatus: self.MaritalStatus,
EmailID: self.Email,
Address: self.Address,
City: self.City,
Reference: self.Reference
};
//Declare an ObservableArray for Storing the JSON Response
self.Employees = ko.observableArray([]);
//Function to perform POST (insert Employee) operation
this.save = function () {
//Ajax call to Insert the Employee
$.ajax({
type: "POST",
url: "/Exercise/Save/",
data: ko.toJSON(this), //Convert the Observable Data into JSON
contentType: "application/json",
success: function (data) {
alert(data);
window.close();
// opener.location.reload(true);
},
error: function () {
alert("Failed");
}
});
//Ends Here
};
}
ko.applyBindings(new EmpViewModel());
</script>
我的表单中存在各种验证器,但没有任何作用。即使没有填写任何字段也正在提交表单?
有什么想法可以解决这个问题吗?