Good day everybody. I have a question about how to use the right way to save data into SQL database through KnockoutJs. The record are display well in the table. It should be able to save the data via this pop-up Modal. But after I click the Create button in that modal, it only pop-up a failed Message. Can anybody please help me to solve this problem? Thank you very much.
Below is extract from main js file about Save function
var data = ko.toJSON(self.Profiles()); $.ajax({ type: 'POST', url: '/ajaxCall/insertProAjax', data: "{ Customer:" + ko.utils.stringifyJson(self.Name) + ",customerRemove:" + ko.utils.stringifyJson(self.CustomerRemove) + "}", contentType: "application/json", success: function (data) { alert("Record has been saved Successfully"); MarkCustomerAsSaved(); $('#AddNewModel').modal('hide'); }, error: function () { alert("Failed"); } }).fail(function (xhr, textStatus, err) { alert(err); });
Below is extract from the ViewModel about save function
var Customer = {}; Customer.Id = c.Id; Customer.Name = c.Name; Customer.Age = c.Age; Customer.Address = c.Address; if (isNewRecord === false) { $.ajax({ type: "PUT", url: "/api/CustomerAPI/" + c.Id, data: Customer }) .done(function (resp) { self.Message("Record Updated Successfully "); self.reset(); }) .fail(function (err) { self.Message("Error Occures, Please Reload the Page and Try Again " + err.status); self.reset(); }); } if (isNewRecord === true) { isNewRecord = false; $.ajax({ type: "POST", url: "/api/CustomerAPI", data: Customer }) .done(function (resp) { self.Message("Record Added Successfully "); self.reset(); loadData(); }).fail(function (err) { self.Message("Error Occures, Please Reload the Page and Try Again " + err.status); self.reset(); }); }