I have a div that is loaded with list of partials:
@foreach (var comment in Model.Comments)
{
Html.RenderPartial("~/Views/Comment/_CommentView.cshtml", comment);
}
When user scrolls down to the bottom of the page I load new comments in that div:
public ActionResult LoadMore(int pageIndex)
{
int _pageSize = 10;
var model = ... get comments
return Json(model.ToList(), JsonRequestBehavior.AllowGet);
}
In page jquery function I add new comments like
for (var i = 0; i < response.length; i++) {
ctrls.push("<div class='comment'> <h4>" +"response[i].CommentText" + "</h4></div>");
}
But this code is not reusable at all so I want to make it better.
Is it possible to:
1) Somehow render partial in JS function and pass data in?
2) Return list of partials from ActionMethod and just add it from JS function?
load()method, but you are already trying to pass back JSON, not HTML. I would instead create a Web API controller, call it with AJAX and return the list of comments for rendering (Web API will take care of the serialization to Json for you).