在我的项目中,我通过搜索进行过滤。我在 API 端查询时可以获取数据,但在 MVC 端消费 API 时没有数据。
API 控制器,
[HttpGet("ListExpenseByAccountingSearch/{search}")]
public async Task<IActionResult> ListExpenseByAccountingSearch(string search)
{
var values = await _expenseService.itemListExpenseByAccountingSearch(search);
return Ok(values);
}
MVC 控制器,
[HttpGet]
public async Task<IActionResult> ListExpenseByAccountingSearch(string search)
{
var client = _httpClientFactory.CreateClient();
var responseMessage = await client.GetAsync($"https://localhost:7198/api/Expense/ListExpenseByAccountingSearch/{search}");
if (responseMessage.IsSuccessStatusCode)
{
var jsonData = await responseMessage.Content.ReadAsStringAsync();
var values = JsonConvert.DeserializeObject<List<ResultExpenseByAccountingDto>>(jsonData);
return Json(values);
}
return Json(new List<ResultExpenseByAccountingDto>());
}
搜索输入,
<input class="form-control form-control-sm" id="txtSearch" type="text" oninput="listExpenseBySearch()" placeholder="Search...">
阿贾克斯,
function listExpenseBySearch() {
var searchText = $("#txtSearch").val();
$.ajax({
type: "GET",
url: "/Management/Accounting/ListExpenseByAccountingSearch/" + searchText,
success: function (value) {
console.log(value);
$("#tableExpense tbody").empty();
var tablerow;
$.each(value, (index, item) => {
tablerow = $("<tr/>");
tablerow.append(`<td><a class="fw-semibold text-primary">#${item.periodID}</a></td>`)
tablerow.append(`<td>${item.createUser}</td>`)
tablerow.append(`<td>${item.periodMonth}</td>`)
tablerow.append(`<td>${item.periodYear}</td>`)
switch (item.paymentStatus) {
case 9999:
tablerow.append(`<td><span class="badge bg-warning-transparent">Bekleniyor</span></td>`)
break;
case 9998:
tablerow.append(`<td><span class="badge bg-warning-transparent">Onay Sürecinde - Yönetici</span></td>`)
break;
case 9997:
tablerow.append(`<td><span class="badge bg-warning-transparent">Onay Sürecinde - Muhasebe</span></td>`)
break;
case 1:
tablerow.append(`<td><span class="badge bg-warning-transparent">Ödeme Bekleniyor</span></td>`)
break;
case 2:
tablerow.append(`<td><span class="badge bg-success-transparent">Ödeme Yapıldı</span></td>`)
break;
case 3:
tablerow.append(`<td><span class="badge bg-dark-transparent">Gecikmiş Ödeme</span></td>`)
break;
case 4:
tablerow.append(`<td><span class="badge bg-danger-transparent">İptal Edilen Ödeme</span></td>`)
break;
}
let formattedAmount = parseFloat(item.totalAmount).toLocaleString('tr-TR', { minimumFractionDigits: 2 });
tablerow.append(`<td>₺${formattedAmount}</td>`)
tablerow.append(`<td><a href="#" class="btn btn-primary-light btn-icon btn-sm"><i class="ri-eye-line"></i></a> <a href="/Management/Accounting/ExpenseSummary/${item.expenseID}" class="btn btn-primary-light btn-icon btn-sm"><i class="ri-send-plane-2-line"></i></a></td>`)
$("#tableExpense").append(tablerow);
})
},
error: function (xhr, status, error) {
console.log(xhr, status, error);
}
});
}
我使用 Postman 获取数据,但 MVC 端的表或控制台中没有数据。它也不会返回任何错误。
这个API没有返回任何东西,因为你没有正确发送它,端点总是得到空字符串并且找不到东西
type: "GET",
url: "/Management/Accounting/ListExpenseByAccountingSearch?search=" + searchText