当我们在jquery中调用.getJson时,我们想要一些关于为什么没有填充对象的List(下面)的指导。
我们在c#中有以下内容。
public class NCListModel
{
public NCListModel()
{
}
public List<NCField> ActiveColumns { get; set; }
public string IdColValue { get; set; }
public string IdColText { get; set; }
public int ID { get; set; }
public string ListName { get; set; }
public int PageNumber { get; set; }
public int RecordsPerPage { get; set; }
public int TotalRecords { get; set; }
public int TotalPages { get; set; }
public string SortBy { get; set; }
public string SortDir { get; set; }
public string SearchBy { get; set; }
public string SearchTerm { get; set; }
public byte ViewMode { get; set; }
}
[Serializable]
[ProgId("NCField_NET.NCField")]
public class NCField
{
public NCField();
public bool CanShow { get; set; }
public string Caption { get; set; }
public short Col { get; set; }
public short ColLen { get; set; }
public short DataType { get; set; }
public short FieldLength { get; set; }
public string FieldName { get; set; }
public bool Hidden { get; set; }
public string ImageName { get; set; }
public bool IsDataField { get; }
public bool IsDefaultFilter { get; set; }
public bool IsFilter { get; set; }
public bool IsID { get; set; }
public bool IsLocation { get; set; }
public bool IsSalesRep { get; set; }
public bool IsSearchFilter { get; set; }
public short LookupListID { get; set; }
public string Name { get; set; }
public string ValueSQL { get; set; }
}
在jQuery中我们为反序列化赋值
var listinfo = {"ActiveColumns":[{"IsSalesRep":false,"IsLocation":false,"ValueSQL":null,"Name":"FullName","DataType":1,"Col":0,"FieldLength":0,"Caption":"Name","FieldName":"Customer.FullName","Hidden":false,"CanShow":true,"IsDataField":true,"IsFilter":true,"IsID":true,"IsDefaultFilter":false,"ColLen":0,"ImageName":"","IsSearchFilter":true,"LookupListID":0},{"IsSalesRep":false,"IsLocation":false,"ValueSQL":null,"Name":"CompanyName","DataType":1,"Col":2,"FieldLength":0,"Caption":"Company","FieldName":"Customer.CompanyName","Hidden":false,"CanShow":true,"IsDataField":true,"IsFilter":true,"IsID":false,"IsDefaultFilter":false,"ColLen":0,"ImageName":"","IsSearchFilter":true,"LookupListID":0},{"IsSalesRep":false,"IsLocation":false,"ValueSQL":null,"Name":"BillAddr2","DataType":1,"Col":6,"FieldLength":0,"Caption":"Bill Addr2","FieldName":"Customer.BillAddr2","Hidden":false,"CanShow":true,"IsDataField":true,"IsFilter":true,"IsID":false,"IsDefaultFilter":false,"ColLen":0,"ImageName":"","IsSearchFilter":true,"LookupListID":0},{"IsSalesRep":false,"IsLocation":false,"ValueSQL":null,"Name":"BillCity","DataType":1,"Col":7,"FieldLength":0,"Caption":"Bill City","FieldName":"Customer.BillCity","Hidden":false,"CanShow":true,"IsDataField":true,"IsFilter":true,"IsID":false,"IsDefaultFilter":false,"ColLen":0,"ImageName":"","IsSearchFilter":true,"LookupListID":0},{"IsSalesRep":false,"IsLocation":false,"ValueSQL":null,"Name":"BillZip","DataType":1,"Col":8,"FieldLength":0,"Caption":"Bill Zip","FieldName":"Customer.BillZip","Hidden":false,"CanShow":true,"IsDataField":true,"IsFilter":true,"IsID":false,"IsDefaultFilter":false,"ColLen":0,"ImageName":"","IsSearchFilter":true,"LookupListID":0},{"IsSalesRep":false,"IsLocation":false,"ValueSQL":null,"Name":"BillState","DataType":1,"Col":9,"FieldLength":0,"Caption":"Bill State","FieldName":"Customer.BillState","Hidden":false,"CanShow":true,"IsDataField":true,"IsFilter":true,"IsID":false,"IsDefaultFilter":false,"ColLen":0,"ImageName":"","IsSearchFilter":true,"LookupListID":0},{"IsSalesRep":false,"IsLocation":false,"ValueSQL":null,"Name":"NCCustomerID","DataType":3,"Col":99,"FieldLength":0,"Caption":"ID","FieldName":"Customer.NCCUstomerID","Hidden":false,"CanShow":false,"IsDataField":true,"IsFilter":false,"IsID":true,"IsDefaultFilter":false,"ColLen":0,"ImageName":"","IsSearchFilter":false,"LookupListID":0}],"IdColValue":"NCCustomerID","IdColText":"FullName","ID":120,"ListName":"Customer","HTML":null,"PageNumber":0,"RecordsPerPage":0,"TotalRecords":0,"TotalPages":0,"SortBy":"BillZip","SortDir":"asc","SearchBy":"FullName","SearchTerm":"","ViewMode":0}
we call this function in jquery.
function LoadData(st) {
listinfo.SearchTerm = st;
// need paging also.
$.ajax({
type: 'GET',
contentType: "application/json; charset=utf-8",
dataType: "json",
url: uLink,
data: listinfo,
async: false,
success: function (json) {
if (json.Message === undefined)
{ $('#table-body').html(json.HTML); }
else {
alert(json.Message);
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert(jqXHR.responseText);
}
});
}
我们看到listinfo是jQuery中的完整对象。
控制器接收具有适当数量的ActiveColumns的对象,但不会填充单个NCField对象的任何属性。
public JsonResult LoadData(NCListModel data)
{.....}