0

I'm trying to post a single object data to an MVC Controler using JQuery, Below are my codes.

 //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);
            }
        });

Below is my controller.

[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
        });
    }

The issue is, the data is not been posted to the controller, the controller receives a null object. Kindly assist, would really appreciate your effort, thanks.

5
  • You're sending GroupData yet the argument is contributionGroupData. You also may not need that outer structure depending on what your C# class looks like. Commented Sep 12, 2018 at 9:05
  • @RoryMcCrossan I have set it to contributionGroupData, and it still doesn't work... Commented Sep 12, 2018 at 9:12
  • 1
    Its just data: GroupData, and remove the contentType option Commented Sep 12, 2018 at 9:14
  • And if you have generated you view correctly, then all you need to var GroupData = $(yourForm).serialize(); Commented Sep 12, 2018 at 9:15
  • And if you did use contentType: 'application/json (which is unnecessary), then it would be data: JSON.stringify({ GroupData: GroupData }), or data: JSON.stringify(GroupData); Commented Sep 12, 2018 at 9:17

2 Answers 2

2

Try Like this:

//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);
            }
        });
Sign up to request clarification or add additional context in comments.

3 Comments

Side note - content: "application/json;", is invalid - its contentType - but its fortunate that you have that typo because it would not work if you used contentType
@StephenMuecke No. I test ,doesnt work with contentType .
Yes I know - that is exactly what I stated - but you have content: "application/json;", in your code (which fortunately is invalid and ignored - there is no ajax option named content)
-1

in your controller your dont have custom binding to bind JSON to your model thats why you get null in you parameter. instead just post it as query, try simply changes your ajax option like so:

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

        ...
}

and perhaps property names are case sensitive so you will need to change your javascript model's name

1 Comment

The $.param is not necessary (and the DefaultModelBinder is not case sensitive)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.