我在控制器中捕获帖子,但String formData
为空。因此,我也无法使用Anti Forgery属性,因为似乎所有数据都丢失了。也许我不应该使用serialzeArray
?
我的帖子:
$("#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
});
我的控制器:
[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 });
}
}
答案 0 :(得分:1)
您发布的是json对象,而不是字符串。
因此,您可以在技术上将json转换为字符串:
var jsonStr = JSON.stringify(json);
请尝试使用FormCollection:
,而不是这样做public ActionResult UpdateCommunicationSettings(FormCollection formData)
{
}
您需要删除序列化。
P.S。你可以使用一个与你客户端上的json具有相同模式的类,它将处理绑定。