我的ViewModel是:
public class CFUViewModel
{
public int RID { get; set; }
public int Order { get; set; }
public List<VCItem> VItems{ get; set; }
}
其中VCItem是:
public class VCItem
{
public string Name{ get; set; }
public string Category { get; set; }
}
在我看来,我正在从表单元素中收集数据。因此,
$('#submit').click(function () {
console.log(gatherData());
transmitData(gatherData());
});
function gatherData() {
var vc = dataObject.getVCItems();
//This is a knockout function that gives an array of VCItems
var data = new Object();
data.RID = $('#rid').val();
data.VItems = vc;
return data;
};
function transmitData(dataToSend) {
$.ajax({
url: '../CFUDashboard/ReceiveData',
type: 'post',
dataType : 'json',
success: function (data) {
alert('success');
},
data: dataToSend
});
};
在控制器中,我有:
[HttpPost]
public ActionResult ReceiveData(CFUViewModel viewModel)
{
//...
}
问题是,虽然RID没问题,但是VItems列表没有被序列化。我怀疑,原因是,附加到JS对象的数组实际上是字符串(数组用引号括起来)。这由console.log(gatherData())
输出为:
VItems : "[{"Name":"OPV3","Category":"FUGY"},{"Name":"OPV7","Category":"FUGX"}]"
没有将列表序列化的原因是什么?我错过了什么?