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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.