knockout.js - KnockoutJs & Asp.Net MVC 4 - multi model views Submit -
i in situation have 2 tabs , each tab have partial views, using jquery ui create tabs.
i have 2 options each user 1. save, 2. submit.
on save save particular tab ( tab contains model view 1) second same ( tabe contains model view 2 )
how can submit both models both tabs 1 click?
i have in save tab 1 , tab 2.
my first view model.
self.save = function() { $.ajax({ url: "mytabs", data: { data: ko.tojson(self.firstvm) }, type: "post", contenttype: "application/json", success: function(result) { alert(result); } }); };
my second view model.
self.save = function() { $.ajax({ url: "mytabs", data: { data: ko.tojson(self.secondvm) }, type: "post", contenttype: "application/json", success: function(result) { alert(result); } }); };
following on comment. here fiddle first approach.
http://jsfiddle.net/sujesharukil/fdpan/
var child1viewmodel = function(){ var x = ko.observable(1), y = ko.observable('child1'), getdata = function(){ return json.stringify({x: x(), y: y()}); }, save= function(){ alert(getdata()); //some ajax call }; return { x: x, y: y, save: save, getdata: getdata } }; var child2viewmodel = function(){ var x = ko.observable(2), y = ko.observable('child2'), getdata = function(){ return json.stringify({x: x(), y: y()}); }, save= function(){ alert(getdata()); //some ajax call }; return { x: x, y: y, save: save, getdata: getdata } }; var parentviewmodel = function(){ var child1 = new child1viewmodel(), child2 = new child2viewmodel(), submit = function(){ var data = { child1data: child1.getdata(), child2data : child2.getdata() }; alert(json.stringify(data)); //make ajax submit }; return { child1: child1, child2: child2, submit: submit } } ko.applybindings(new parentviewmodel());
and html be
<form data-bind="submit: submit"> <h1>data child 1</h1> x = <input data-bind="value: child1.x"/><br/> y = <input data-bind="value: child1.y"/><br/> <button type="button" data-bind="click: child1.save">save child1</button> <h1>data child 2</h1> x = <input data-bind="value: child2.x"/><br/> y = <input data-bind="value: child2.y"/><br/> <button type="button" data-bind="click: child2.save">save child2</button><br/> <button type="submit">submit</button> </form>
hope helps.
Comments
Post a Comment