The problem is here:
data: {
letterViewModel: $('#letterForm').serialize(),
relatedLetters: JSON.stringify(relatedLetterList)
},
The .serialize()
method already performs an application/x-www-form-urlencoded
serialization of the data and jQuery data does another one so you end up with invalid request.
You could include a hidden field in your #letterForm
and set its value immediately before posting the form:
$('#letterForm').submit(function (e) {
e.preventDefault();
// set the value of the hidden field to the corresponding JSON
$('#relatedLetters').val(JSON.stringify(relatedLetterList));
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
alert('success ' + result);
},
error: function () {
alert('error');
}
});
});
Also notice that I have used the .submit event of the form instead of the .click event of a submit button. The reason for this is that a form can be submitted in other ways, not only by clicking on the submit button with a mouse. Think for example the user pressing the Enter key while inside some iof the form fields. This will submit the form and if you subscribe to the click event of your submit button your AJAX request will never fire.