Click here to Skip to main content
15,904,926 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi Guys,

I have the following action on a controller:
C#
[HttpPost]
public ActionResult SaveSubmission(SubmissionModel submission, SubmissionStateEnum submissionState)
{
    if (Request.IsAjaxRequest())
    {
        return Json(true, JsonRequestBehavior.AllowGet);
    }
    return RedirectToAction("Index");
}


The submission model looks as follows:

C#
[Serializable]
public class SubmissionModel
{
    public string TestString { get; set; }
    public SubmissionDetailsModel Details { get; set; }
    public SubmissionIncomeStatement IncomeStatement { get; set; }
    public SubmissionSubcontractorModel SubcontractorDetails { get; set; }

    public SubmissionModel()
    {
        TestString = "NEW";
        Details = new SubmissionDetailsModel();
        IncomeStatement = new SubmissionIncomeStatement();
        SubcontractorDetails = new SubmissionSubcontractorModel();
    }
}


On my page, I have this function to submit the data:

JavaScript
function SaveSubmission() {
           
            var sd = { MarketId: 2, SubmissionDate: $('#submissionDate').attr('data-date') };
            
            var so = {
                TestString: 'fromPage',
                Details: sd,
                IncomeStatement: null,
                SubcontractorDetails: null
            };

            $.ajax({
                type: "POST",
                url: '@Url.Action("SaveSubmission")',
                data: { submission: JSON.stringify(so), submissionState: '@SubmissionStateEnum.Rejected' },
                dataType: "json",
                success: function (data) {
                    alert(data);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR);
                    alert(textStatus);
                    alert(errorThrown);
                }
            });

        }


The problem I am having is that when the action gets called from the ajax the "submission" parameter is null, however if I look at the request I can see it has passed the data back.
So my question is why is it not mapping to my object on the action? I've tried serialisation and I cannot seem to get it to work? I might be missing something very obvious but I have no idea what it is.

If you need more info, please ask.

Thanks in advance.
Posted

use this code

C#
function SaveSubmission() {

            var sd = { MarketId: 2, SubmissionDate: $('#submissionDate').attr('data-date') };

            var so = {
                TestString: 'fromPage',
                Details: sd,
                IncomeStatement: null,
                SubcontractorDetails: null
            };

            $.ajax({
                type: "POST",
                url: '@Url.Action("SaveSubmission")',
                data: { submission: so, submissionState: '@SubmissionStateEnum.Rejected' },
                dataType: "json",
                success: function (data) {
                    alert(data);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR);
                    alert(textStatus);
                    alert(errorThrown);
                }
            });

        }
 
Share this answer
 
Comments
Pheonyx 24-Apr-14 9:23am    
That didn't work, by didn't work I mean that the 'so' object that I received on my controller was just a blank instantiation and did not contain any of the details set in the jquery function.
update the ajax code as below

C#
function SaveSubmission() {

            var sd = { MarketId: 2, SubmissionDate: $('#submissionDate').attr('data-date') };

            var so = {
                TestString: 'fromPage',
                Details: sd,
                IncomeStatement: null,
                SubcontractorDetails: null
            };

            $.ajax({
                type: "POST",
        contentType: "application/json",
                url: '@Url.Action("SaveSubmission")',
                data: "{ submission:"+ JSON.stringify(so) +", submissionState:"+ '@SubmissionStateEnum.Rejected' +" }",
                dataType: "json",
                success: function (data) {
                    alert(data);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR);
                    alert(textStatus);
                    alert(errorThrown);
                }
            });

        }
 
Share this answer
 
Comments
Pheonyx 25-Apr-14 3:53am    
The "contentType: 'application/json', " line fixed the issue. Thanks for that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900