我在客户端有一个具有 SettingName 和 SettingValue 属性的 javascript 对象。
数据示例 -
obj1.SettingName = "createdDate";
obj1.SettingValue = "10/07/2013";
obj2.SettingName = "arrayOfNames";
obj1.SettingValue = SomeArray; //An array of 5 strings
var settingsArray =new Array();
settingsArray[0] = obj1;
settingsArray[1] = obj2;
var settingsContainer = new Object;
settingsContainer.Settings = settingsArray;
ajax 调用 -
$.ajax({
type: "POST",
url: "myURL",
data: { settings: JSON.stringify(settingsContainer)},
dataType: 'json',
success: function (data) {
//do something
}
});
服务器端类 -
public class Setting
{
public string SettingName { get; set; }
public object SettingValue { get; set; } //so this can hold multiple data types
}
public class SettingsContainer
{
public List<Setting> Settings { get; set;}
}
控制器动作 -
public ActionResult myURL(SettingsContainer settings) {
//Here, the value for SettingsContainer.Settings[1].SettingValue, which
should be Array[5] i.e. on the C# side, I expect to see Object[5],
is only {object}. I'm not sure what's going on.
Is my approach of handling this correct?
}
任何建议/帮助将不胜感激。谢谢!