There's probably a nicer way of doing it than this, but this is what I've put together in about 5 minutes. In this example, I'm posting in a list of Person objects.
public class Person
{
public string Title { get; set; }
public string Forename { get; set; }
public string Surname { get; set; }
}
The controller class is as follows:
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string people)
{
var serializer = new Newtonsoft.Json.JsonSerializer();
var data = serializer.Deserialize<list><person>>(
new JsonTextReader(
new TextBufferReader(
new SeekableTextReader(people))));
return View();
}
}</person></list>
And finally the HTML and JavaScript:
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<div>
<form action="/Home/Index" method="POST">
<input id="people" name="people" type="text"/>
<button type="button" data-bind="click: submit">Submit</button>
</form>
</div>
<script src="/Scripts/jquery-2.1.1.min.js" type="text/javascript"></script>
<script src="/Scripts/knockout-3.1.0.js" type="text/javascript"></script>
<script type="text/javascript">
var Person = function(title, forename, surname) {
this.title = ko.observable(title);
this.forename = ko.observable(forename);
this.surname = ko.observable(surname);
}
this.people = ko.observableArray();
this.people.push(new Person("Mr", "Bob", "Marley"));
this.people.push(new Person("Mrs", "Grace", "Jones"));
this.people.push(new Person("Mr", "Mick", "Jagger"));
function submit() {
$("#people").val(JSON.stringify(ko.toJS(this.people())));
$("form").submit();
}
ko.applyBindings(this);
</script>
</body>
</html>