My I.T. Side

It's all about web [ASP.NET MVC]

How to pass data from knockoutjs to ASP.NET MVC view model

You can pass a data using your model in knockoutjs to ASP.NET MVC View model as argument in your action methods.  

First create your model in knockoutjs

var model = function(){
     var self = this:
     self.FirstName = ko.observable();
     self.LastName = ko.observable();
};

Then use this knockout model to pass the data through ajax.

var viewModel = function(){
     var self = this:

     self.Param = new model();
 
     self.SendData = function(){
	var param = ko.toJS(self.Param);
	$.ajax({
            url: "controller/CreateData",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data: ko.utils.stringifyJson(param),
            success: function (result) {
                //success process in here...
            }
        });
     };
};

Create a viewmodel class:

public class DataViewModel{
    public string FirstName{get;set;}
    public string LastName{get;set;}
}

And in your controller

public ActionResult CreateData(DateViewModel param) {
	//process here..
}

Now you can get the data from args param and use that for your transactions.


Happy Coding