I'm encountering an issue while attempting to send data back to my save method using ajax. I prefer this method because it allows me to reuse the form on multiple screens by rendering it as a partial page with the same save function.
The problem I'm facing is that when I use this approach, it sends the data back to my save function but the serialized JSON is created during page load and does not capture the updated values. I would like the updated values to be sent back via ajax without having to manually capture every field's value on the page.
Below is the current code I am using which successfully sends the original values from the model back to my save method:
$("#Save").click(function () {
var form = $('#Form');
if (form.valid()) {
var data = '@Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(@Model))';
$.ajax({
url: "/Save",
type: 'POST',
data: JSON.stringify({ modelJson: data }),
contentType: 'application/json',
success: function (result) {
SaveFade();
}
});
}
return false;
});