Abhineet Srivastava

Abhineet Srivastava

  • NA
  • 890
  • 94.7k

Save item to Database using Knockout.js

Sep 24 2013 3:26 AM
Hi All,

I have add the record into the list using Knckout.js but now i have to save this new record to database.

Anyone have a idea?

My code is below-
-----------------

.Cshtml
------

<table data-bind="template: { name: 'emp-template', foreach: Students }" class="formatHTML5">
    <thead>
        <tr>
            <td>First Name</td>
            <td>Last Name</td>
            <td>Middle Name</td>
        </tr>
    </thead>
    <!-- <tfoot>
        <tr>
            
        </tr>
    </tfoot> -->

</table>
<input data-bind="value: firstName" />
<input data-bind="value: lastName" />
<input data-bind="value: middleName" />
<button type="button" data-bind="click: addNewRow">add row</button>
<button data-bind='click: save'>Save</button>
<script type="text/html" id="emp-template">
    <tr>
        <td class="td-center">
            <span data-bind="text: FirstName"></span>
        </td>
        <td class="td-center"><span data-bind="text: LastName"></span>
        </td>
        <td class="td-center">
            <span data-bind="text: MiddleName"></span>
        </td>
    </tr>
</script>


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Viewmodel
-----------

var AppViewModel = function () {
    var self = this;

    self.firstName = ko.observable();
    self.lastName = ko.observable();
    self.middleName = ko.observable();
    self.Students = ko.observableArray([]);

    self.addNewRow = function () {
        //alert("First Name Is");
        //var firstName = self.firstName.trim();
        //var lastName = self.lastName.trim();
        //var middleName = self.MiddleName.trim();
        self.Students.push({ FirstName: self.firstName(), LastName: self.lastName(), MiddleName: self.middleName() });
    };
}
$(document).ready(function () {
    var viewModel = new AppViewModel();
    $.getJSON('/ajaxCall/getEmpAjax/', function (data) {
        viewModel.Students(data);
        ko.applyBindings(viewModel);
    });

});


Answers (1)