I have a form created with Knockout.js. When the user presses the submit button, I convert the viewmodel back in a model and am trying to submit to the server. I tried:
ko.utils.postJson(location.href, ko.toJSON(viewModel));
But the object was blank when it hit the server. I switched to this code:
$.ajax({
url: location.href,
type: "POST",
data: ko.toJSON(viewModel),
datatype: "json",
contentType: "application/json charset=utf-8",
success: function (data) { alert("success"); },
error: function (data) { alert("error"); }
});
That gets the data to the server with the correct data in it.
But what I would like is to have the data submitted so my controller can redirect to the correct view. Any suggestions?
Also, it's in the example mentioned, but you may need to alter your JavaScript call to something like:
Steve Sanderson has an older sample that demonstrates getting submitted JSON data to be bound properly in your controller action here: http://blog.stevensanderson.com/2010/07/12/editing-a-variable-length-list-knockout-style/
The gist of it is that he creates an attribute called "FromJson" that looks like:
Then, the action looks like:
Now, you could use ko.utils.postJson to submit your data and respond with an appropriate view.