0

I'm catching the post in my controller, but the String formData is null. Because of this, I'm also unable to use the Anti Forgery Attribute, since it seems like all the data is lost. Maybe I shouldn't be using serialzeArray?

My Post:

    $("#SetCommunicationSettingsForm").submit(function (e) {
            debugger;
            var postData = $(this).serializeArray();
            var formURL = $(this).attr("action");
            $.ajax(
            {
                url: formURL,
                type: "POST",
                datatype:"json",
                data: postData,
                success: function (data) {
                    debugger;
                    //data: return data from server
                    if (data.success == true) {
                        $("#SaveSuccessful").show();
                        $("#SetCommunicationSettingsFormArea").hide();
                        $("#SumbitCommunicationSettingForm").val('<%: ServiceSite.Resources.Resources.COMMON_CLOSE %>');
                        $("#SumbitCommunicationSettingForm").unbind('click');
                        $("#SumbitCommunicationSettingForm").click(closeCommunicationSettingPopup);
                        $("#CancelCommuncationSettingButton").hide();
                    }
                    else {
                        $("#DeviceCommuncationSettingLocation").html(data);
                    }
                }
            });
            e.preventDefault(); //STOP default action
        });

My controller:

  [HttpPost]
    //[AuthorizeActionFilter]
    //[ValidateAntiForgeryToken (Salt= "UpdateCommunicationSettings")]
    public ActionResult UpdateCommunicationSettings(String formData)
    {
        //Decode parameters 
        JavaScriptSerializer ser = new JavaScriptSerializer();
        Dictionary<String, Object> jsonParams = ser.Deserialize<Dictionary<String, Object>>(formData);

        //verify permissions on server side
        if (((List<string>)Session["AuthorizedActions"]).Contains("EditDeviceCommunicationSettings"))
        {
            // Build DeviceData.DeviceConfigurationSettings from model, pass to DAL to Save
            List<long> deviceIds = GetSelectedDevicesFromSession();

            foreach (long id in deviceIds)
            {

            }

            return PartialView("DeviceCommunicationSettingDialog");
        }
        else
        {
            return Json(new { notAuthorized = true });
        }
    }

1 Answer 1

1

You are posting a json object and not a string.
So you can technically convert you json into a string with:

var jsonStr = JSON.stringify(json);

Instead of doing that, try using FormCollection:

public ActionResult UpdateCommunicationSettings(FormCollection formData)
{
}

You need to drop the serializing.

P.s. you can use a class with the same schema as the json you have on you client, and it will deal with the binding.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.