ajax asp.net list 展开,javascript - How To create a Comments list with ajax in asp.net core? - Stack O...

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

@foreach (var comment in Model.Comments)

{

@comment.Content@comment.CreateTime

}

Load more

@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

Aa8Lv.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值