Javascript 使用jQuery Ajax将单个对象传递到MVC控制器方法

Javascript 使用jQuery Ajax将单个对象传递到MVC控制器方法,javascript,c#,jquery,asp.net-mvc-5,Javascript,C#,Jquery,Asp.net Mvc 5,我试图使用JQuery将单个对象数据发布到MVC控制器,下面是我的代码 //declare of type Object of GroupData var GroupData = {}; //pass each data into the object GroupData.groupName = $('#groupName').val(); GroupData.narration = $('#narration').val(); GroupData.

我试图使用JQuery将单个对象数据发布到MVC控制器,下面是我的代码

 //declare of type Object of GroupData
    var GroupData = {};
    //pass each data into the object 
    GroupData.groupName = $('#groupName').val(); 
    GroupData.narration = $('#narration').val();
    GroupData.investmentCode = $('#investmentCode').val();
    GroupData.isNew = isNewItem;
    //send to server
        $.ajax({
            url: "/Admin/SaveContributionInvestGroup",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            dataType: "json", 
            data: JSON.stringify({ GroupData: JSON.stringify(GroupData) }),

            success: function (res) {
                alertSuccess("Success", res.Message);

                //hide modal
                $('#product-options').modal('hide');

                hide_waiting();
            },
            error: function (res) {
                alertError("Error", res.Message);
            }
        });
下面是我的控制器

[HttpPost]
    public JsonResult SaveContributionInvestGroup(ContributionInvestmentGroup GroupData)
    {
        ClsResponse response = new ClsResponse();
        ClsContributionInvestmentGroup clsClsContributionInvestmentGroup = new ClsContributionInvestmentGroup();

        var userID = (int)Session["userID"];
        var sessionID = (Session["sessionID"]).ToString();

        if (contributionGroupData != null)
        {
            //get the data from the cient that was passed
            ContributionInvestmentGroup objData = new ContributionInvestmentGroup()
            {
                contributionInvestmentGroupID = 0,
                groupName = GroupData.groupName,
                narration = GroupData.narration,
                investmentCode = GroupData.investmentCode,
                isNew = GroupData.isNew
            };

            response = clsClsContributionInvestmentGroup.initiateNewContributionInvestmentGroup(sessionID, objData);
        }
        else
        {
            response.IsException = true;
            response.IsSucess = false;
            response.Message = "A system exception occurred kindly contact your Administrator.";
        }


        return Json(new
        {
            response.IsSucess,
            response.Message
        });
    }
问题是,数据未发布到控制器,控制器接收到空对象。 非常感谢您的帮助。

请尝试以下方式:

//send to server
        $.ajax({
             type: "POST",
                url: "/Admin/SaveContributionInvestGroup",
                dataType: "json",
                data:  GroupData,
            success: function (res) {
                alertSuccess("Success", res.Message);

                //hide modal
                $('#product-options').modal('hide');

                hide_waiting();
            },
            error: function (res) {
                alertError("Error", res.Message);
            }
        });

在您的控制器中,您没有
自定义绑定
将JSON绑定到您的模型,这就是为什么您的参数中会出现null。 与其将其作为查询发布,不如简单地更改ajax选项,如下所示:

{
        ...
        contentType: "application/x-www-form-urlencoded", //default: 
        ..., 
        data: $.param(GroupData),

        ...
}

也许属性名是区分大小写的,因此您需要更改您发送的
GroupData
的javascript模型名,但参数是
contributionGroupData
。根据您的C#类的外观,您可能也不需要该外部结构。@RoryMcCrossan我已将其设置为contributionGroupData,但它仍然不起作用……它只是
data:GroupData,
并删除
contentType
选项,如果您生成的视图正确,则需要
var GroupData=$(yourForm).serialize();
如果确实使用了
contentType:'application/json
(这是不必要的),那么它将是
data:json.stringify({GroupData:GroupData}),
data:json.stringify(GroupData);
不需要
$.param
(并且
DefaultModelBinder
不区分大小写)旁注-
content:“application/json;”,
是无效的-它的
contentType
-但幸运的是,你有这样的输入错误,因为如果你使用
contentType
,它将不起作用。我测试,不适用于
contentType
。是的,我知道-这正是我所说的-但你有
内容:代码中的“application/json;”,
(幸运的是,该选项无效且被忽略-没有名为
content
的ajax选项)