So, I hit my head against the wall with this, and I cannot find good sources for this. I may be forgetting how the model binding file works in MVC3, but here's what I'm trying to do: I have an editor associated with Knockout to handle model editing. There are not many in the model:
public class SetupTemplate { public int Id { get; set; } public string Name { get; set; } public string Template { get; set; } }
The signature of the action I'm trying to trigger:
[HttpPost] [ValidateAntiForgeryToken] public ActionResult UpdateTemplate(SetupTemplate template)
From another question here, I took this pretty useful snippet to get the anti-fake token:
window.addAntiForgeryToken = function(data) { data.__RequestVerificationToken = $('#__AjaxAntiForgeryForm input[name=__RequestVerificationToken]').val(); return data; };
What happens to me when I try to post an update via ajax:
payload = window.addAntiForgeryToken(ko.mapping.toJS(self.data)); $.ajax({ type: "post", url: endpoint, data: payload, success: function(data) {
As a result, in the data section of the Chrome Developer Tools form
Id:1 Name:Greeting Template: [Template Text] __RequestVerificationToken: [The really long anti-forgery token]
The anti-corrosion token is confirmed, but my model is zero. Most of the examples I've seen just use only one parameter, not a model.
I am sure that I am missing something obvious, any understanding of what it could be?
EDIT: In response to @Mark, changing the call to this:
$.ajax({ type: "post", dataType: "json", contentType: 'application/json', url: endpoint, data: JSON.stringify(payload), success: function(data) {
Results in the request payload:
{"Id":1,"Name":"Greeting","Template":"...","__RequestVerificationToken":"..."}:
And the server does not pick up the anti-fake token. This has been tested both with and without contentType parameters up to $.ajax() .