我在Html視圖中顯示數據時遇到問題。我創建了web api asp.net mvc項目。在web api控制器中,我們創建了crud方法(get,post,put和delete)。例如,我在web api控制器中有以下方法。在Html視圖中不顯示數據(僅顯示'未定義的數據值)
public IEnumerable<vGeneralLedger> GetAllPosting()
{
try
{
return _db.vGeneralLedgers.ToList();
}
catch (Exception ex)
{
throw;
}
}
該方法將顯示從數據庫的所有發佈。我說'會',因爲我不明白在HTML中。在HTML中,我有以下功能:
function GetAllPosting() {
jQuery.support.cors = true;
$.ajax({
url: 'http://localhost:xxxxx/api/GeneralLedgerJournal/',
type: 'GET',
dataType: 'json',
success: function (data) {
WriteResponse(data);
},
error: function (x, y, z) {
alert(x + '\n' + y + '\n' + z);
}
});
}
function WriteResponse(generalLedgerJournals) {
$.each(generalLedgerJournals, function (index, generalLedgerJournal) {
$("#GLJtable").append('<tr><td width="150px">' + generalLedgerJournal.Id +
'</td><td width="150px">' + generalLedgerJournal.NaturalBusinessYearId +
'</td><td width="150px">' + generalLedgerJournal.DocumentTypeId +
'</td>' + '<td width="150px">'
+ generalLedgerJournal.AccountNo + '</td><td width="150px">' + generalLedgerJournal.PostingDate +
'</td><td width="150px">' + generalLedgerJournal.MaturityDate +
'</td><td width="150px">' + generalLedgerJournal.Description +
'</td><td width="150px">' + generalLedgerJournal.TotalDebit +
'</td><td width="150px">' + generalLedgerJournal.TotalCredit +
'</td><td width="150px">' + generalLedgerJournal.Balance + '</td><td width="50px">' + '</td></tr>');
});
}
在瀏覽器控制檯(當我打F12)我有JSON數據如圖(鏈接)顯示:
https://www.dropbox.com/s/htn5lav9g12c57r/Json%20Result.jpg?dl=0
但在HTML視圖( Cshtml)我總是顯示'undefined'的數值數據,如圖所示(鏈接): https://www.dropbox.com/s/oi52caemjlx9ed3/My%20result%20always%20undefined.jpg?dl=0