MVC vs JSON - passing the data from the client to the controller

New to MVC.

Trying to pass this JSON:

function saveUser() {
        var postData = {            
            'Name': $('#txtLocalName').val(),
            'Address1': $('#txtAddress1').val(),
            'Address2': $('#txtAddress2').val(),
            'City': $('#txtCity').val(),
            'StateID': $('#selState').val(),
            'ZipCode': $('#txtZip').val(),
            'PhoneNumber': $('#txtPhone').val(),
            'FaxNumber': $('#txtFax').val(),                              
            'ParentUnitID': $('#selParentUnit').val(),
            'CraftID': $('#selCraft').val(),
            'CountyID': $('#selCounty').val(),
        };
        $.ajax({
            url: '/Administration/SaveLocalUnionInfo',
            type: 'POST',
            dataType: 'json',
            data: postData,
            success: function (data) {
                if (data.SuccessState) {
                    addEditLocalUnionDialog.dialog("close");
                }
                else {
                    //Step 1 - Build the string
                    var str = data.Message;

                    //Step 2 - Add string to Warning Dialog
                    $('#warningMsgGoesHere').html(str);

                    //Step 3 - Open Warning dialog
                    warningDialog.dialog({
                        title: 'Error Saving User'
                    });
                    warningDialog.dialog('open');
                }
            },
            error: function (data) {
                console.log("Error in saveUser.");
            }
        });

    };

Open in new window


to this Controller method:

  public ActionResult SaveLocalUnionInfo(SystemLocationData data)
        {
         
        }// breakpoint

Open in new window



I am reaching the controller code while debugging ..  but data does not contain any information sent from the client ... none of the fields have values.


I set a breakpoint in the developer tools and postData has data **just before** the call to the controller method.


So either the data isn't making it -- or the datatype is wrong.

How can I debug this?

Is there a type I can use to receive the JSON data in a more generic fashion?  I tried this:

  public ActionResult SaveLocalUnionInfo(object data)
        {
         
        }// breakpoint

Open in new window


but I don't know what to unbox the data object as.
LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?
 
Duy PhamFreelance IT ConsultantCommented:
Your postData doesn't match with the signature of controller method. Your controller method SaveLocalUnionInfo expects a parameter data (of type SystemLocationData).

Try declare your postData as:
var postData = {
            'data': {            
                'Name': $('#txtLocalName').val(),
                'Address1': $('#txtAddress1').val(),
                'Address2': $('#txtAddress2').val(),
                'City': $('#txtCity').val(),
                'StateID': $('#selState').val(),
                'ZipCode': $('#txtZip').val(),
                'PhoneNumber': $('#txtPhone').val(),
                'FaxNumber': $('#txtFax').val(),                              
                'ParentUnitID': $('#selParentUnit').val(),
                'CraftID': $('#selCraft').val(),
                'CountyID': $('#selCounty').val(),[/indent]
            }
        };

Open in new window

0
 
Tom KnowltonWeb developerAuthor Commented:
Thank you.

I also discovered that I needed getters and setters so that JSON could map to the properties:

   
[Serializable]
    public struct SystemLocationData
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string City { get; set; }
        public int StateID { get; set; }      
        public string Zip { get; set; }
        public string PhoneNumber { get; set; }
        public string FaxNumber { get; set; }
        public string EmailAddress { get; set; }
        public int TimeZoneID { get; set; }
        public bool ObservesDaylightSavings { get; set; }
        public int SystemLocationTypesID { get; set; }
        public bool HasOffice { get; set; }        
        public List<string> IPAddresses { get; set; }
        public List<SystemUserWorkAreaData> WorkAreas { get; set; }
        public bool IsLocal { get; set; }
        public bool IsDeleted { get; set; }
        public int ParentUnitID { get; set; }
        public int CraftID { get; set; }
        public int CountyID { get; set; }
        public string LocalNumber { get; set; }
        public string UBCNumber { get; set; }
    }

Open in new window

0
 
Tom KnowltonWeb developerAuthor Commented:
Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.