Reputation: 612
here is the controller
public ViewResult AddNewRow(ProjectBudgetModel model)
{
//Some oprations goes here on the passed model the return to the same view
return View("AddNewProjectBudget", model);
}
here is the view which has the ajax call like
$.ajax({
url: '@Url.Action("AddNewRow", "ProjectBudget")',
type: 'post',
data: {model: '@Model'},
contentType: 'application/json; charset=utf-8;',
dataType: 'json',
success: function (response) {
alert(response.success)
return;
},
error: function (x) {
alert(x.status);
}
});
@Model which is passed in the data header in the ajax call is ProjectBudgetModel
something goes wrong here specifically while i pass the data to the controller it even doesn't hit the brekpoint of the addNewRow function in controller
any help?
Upvotes: 0
Views: 1410
Reputation: 13
The only way I was able to send the data from the view to the model on controller was using:
data: $("#FORM_ID").serialize(),
If I use the: data: JSON.stringify(@Html.Raw(Json.Encode(Model))),
the controller receives a empty model.
Upvotes: 0
Reputation: 1038800
data: {model: '@Model'},
doesn't do at all what you think it does. Look at the generated markup to see that this emits some broken values.
It should be like that:
data: JSON.stringify(@Html.Raw(Json.Encode(Model))),
The JSON.stringify
method is natively built-into modern browsers. If for some reasons you need to support browsers from the stone age, you could include the json2.js script to your page which will define the method.
Upvotes: 2