1

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();
     });
     }
    
asked Jul 13, 2017 at 23:04
6
  • $.ajax wants data, you're giving it Data. Those are not the same. Commented Jul 13, 2017 at 23:14
  • Could you please explain the details? I am a beginner, just learn knockoutJs for 1 week. Thank you. Commented Jul 13, 2017 at 23:18
  • The comment was not knockout-related. JavaScript is case-sensitive, so are JavaScript object keys. data and Data are two different keys. jQuery's ajax function expects data to start with a lowercase letter, just as it expects type, url, contentType and success and not Type, URL, ContentType or Success. Commented Jul 13, 2017 at 23:21
  • Thank you Amadan. I changed the key name from Data to data. I will be more carefully on named. But it doesn't effect the process, there is a console message ".../ajaxCall/insertProAjax 404 (Not Found)". I think the ajax can't call to anywhere. I'm still confuse about it. Commented Jul 13, 2017 at 23:31
  • this might help you stackoverflow.com/questions/4120212/…. I am guessing you are attempting to post to a .net mvc controller? another option would be to include web api in your project and then post to the .net web api controller. (these controllers are meant for this kind of thing) Commented Jul 14, 2017 at 1:16

1 Answer 1

0

Knockout and Javascript (in this manner) are being processed client side. You will need to create something on the back end to accept your data payload and save it to the database. If you want to stay in the JavaScript family, I would recommend node.js. Alternatively this is where php, or C# would come into play.

answered Jul 14, 2017 at 1:07

1 Comment

Yes, I see it, should be add a back end C# file to support the server side. Thank you for remind me.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.