Here is a simple working demo , you could refer to:
Model
public class Comment
{
public int Id { get; set; }
public string Content { get; set; }
public DateTime CreateTime { get; set; }
}
CommentList PageModel
public class CommentListModel : PageModel
{
private readonly SampleContext _context;
public CommentListModel(SampleContext context)
{
_context = context;
}
[BindProperty]
public List Comments { get; set; }
public void OnGet()
{
Comments = _context.Comment.OrderByDescending(c => c.CreateTime).Take(5).ToList();
}
public async Task OnGetAllCommentAsync()
{
Comments = _context.Comment.OrderByDescending(c => c.CreateTime).ToList();
return Partial("_CommentPartial", Comments);
}
}
CommentList page, show 5 comments when page loaded and use ajax to call AllComment method to return Partial view which displays the all of comment data
@page
@model RazorPages3_1.CommentListModel
@section Scripts
{
$("#clicklink").click(function () {
$.ajax({
type: "get",
url: "/CommentList?handler=AllComment",
success: function (response) {
$("#commentlist").html(response);
}
});
});
}
_CommentPartial view
@model List
@foreach (var comment in Model)
{
@comment.Content@comment.CreateTime}
Result
@foreach (var comment in Model.Comments)
{
@comment.Content@comment.CreateTime}
Load more