Reputation: 2599
Using knockout i would like to accept submitted values and send them to server using ko.toJson. I am new to knockout and am sure i having some syntactic issues. More concisely i want my submit button to save the values in my textbox and save them to server. here is my fiddle https://jsfiddle.net/Domt301/30h3oe9t/
<div class='Agent Form'>
<p>First name: <input data-bind='value: firstName' /></p>
<p>Last name: <input data-bind='value: lastName' /></p>
<p>Agent NPN: <input data-bind='value: NPN' /></p>
<p>Lead Location: <input data-bind='value: leadlocation' />(City, State, Zip)</p>
<p>Requested Lead Delivery Time/Date: </p>
<p><input data-bind='value: leaddeliverytime' /></p>
<p>Leads Requested: <select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="10">10</option>
</select> </p>
<p>Email Address: <input data-bind='value: EmailAddress' /></p>
</div>
<div>
<input type="button" id="btnSubmit"
value="Submit" data-bind = "click: submit" />
<input type="button" id="btnReset"
value="Reset" data-bind = "click: reset" />
</div>
script
var ViewModel = function(first, last, NPN, leadlocation, leaddeliverytime, EmailAddress) {
this.firstName = ko.observable(first);
this.lastName = ko.observable(last);
this.NPN = ko.observable(NPN);
this.leadlocation = ko.observable(leadlocation);
this.leaddeliverytime = ko.observable(leaddeliverytime);
this.EmailAddress = ko.observable(EmailAddress);
};
var jsonData = ko.toJson(viewModel);
Upvotes: 1
Views: 1542
Reputation: 7493
I have updated the original fiddle.
You can bind the submit
and reset
buttons to functions inside of your view model. Then create a submit
function inside of your data model where you create a data object, in this context I define it as Agent
and pass that to an ajax call where you would replace /echo/json
with the path to the controller where you are posting the data.
I made a few slight changes to your html to keep track of the observables
.
<div class='Agent Form'>
<p>First name: <input data-bind='value: firstName' /></p>
<p>Last name: <input data-bind='value: lastName' /></p>
<p>Agent NPN: <input data-bind='value: npn' /></p>
<p>Lead Location: <input data-bind='value: leadLocation' />(City, State, Zip)</p>
<p>Requested Lead Delivery Time/Date: <input data-bind='value: leadDeliveryTime' /></p>
<p>Leads Requested:
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="10">10</option>
</select> </p>
<p>Email Address: <input data-bind='value: emailAddress' /></p>
</div>
<div>
<button data-bind = "click: submit" >Submit</button>
<button data-bind = "click: reset" >Reset</button>
</div>
Here is the knockout code:
debugger;
var viewModel = function() {
var self = this;
self.firstName = ko.observable();
self.lastName = ko.observable();
self.npn = ko.observable();
self.leadLocation = ko.observable();
self.leadDeliveryTime = ko.observable();
self.leadsRequested = ko.observable();
self.emailAddress = ko.observable();
self.fullName = ko.computed(function() {
return self.firstName() + " " + self.lastName();
});
self.submit = function () {
var Agent = {};
Agent.FirstName = self.firstName();
Agent.LastName = self.lastName();
Agent.Npn = self.npn();
Agent.LeadLocation = self.leadLocation();
Agent.LeadDeliveryTime = self.leadDeliveryTime();
Agent.LeadsRequested = self.leadsRequested();
Agent.EmailAddress = self.emailAddress();
Agent.FullName = self.fullName();
$.ajax({
url: '/echo/json/',
type: 'POST',
data: Agent,
success: function (result) {
alert("Recorded inserted Sucessfully");
self.firstName("");
self.lastName("");
self.npn("");
self.leadLocation("");
self.leadDeliveryTime("");
self.leadsRequested("");
self.emailAddress("");
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
debugger;
alert("some error");
}
});
};
self.reset = function() {
self.firstName("");
self.lastName("");
self.npn("");
self.leadLocation("");
self.leadDeliveryTime("");
self.leadsRequested("");
self.emailAddress("");
alert("Reset");
};
};
ko.applyBindings(new viewModel());
Upvotes: 1