我正在尝试从 ASP.NET Core 2.0 中的 Razor Pages 获取一些数据。
但问题是它不返回任何数据。我也试过调试它,它根本不会触发那个方法(OnGetProducts)。
模型Index.cshtml.cs:
private IProductRepository _productRepository;
public IndexModel(IProductRepository repository)
{
_productRepository = repository;
}
public void OnGet()
{
}
public IActionResult OnGetProducts(int page)
{
var model = _productRepository.GetProducts().Skip(page * 10).Take(10);
return new JsonResult(model);
}
剃须刀页面 Index.cshtml
<div id="products">
</div>
@section scripts{
<script>
$(function () {
getProducts(0);
});
var isInitialized = false;
function getProducts(page) {
$.ajax({
type: 'GET',
url: "Products",
contentType: "application/json",
dataType: "json",
data: {
handler: 'Products',
page: page
},
success: function (datas) {
console.log(datas);
}
});
}
</script>
}
将此页面放在 Pages/Products/Index.cshtml(.cs) 文件夹中