0

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?

4

2 回答 2

2
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.

于 2013-01-13T12:53:14.233 回答
0

我能够将数据从视图发送到控制器上的模型的唯一方法是使用:

data: $("#FORM_ID").serialize(),

如果我使用:数据:JSON.stringify(@Html.Raw(Json.Encode(Model))),控制器收到一个空模型。

于 2015-10-08T10:49:27.357 回答