Reputation: 397
I am trying to add pagination with infinite scrolling to my web application. I'm just having one problem, which is that I need to pass a value to a controller action method to fetch the next page in the sequence.
The action looks like this:
public ActionResult Index(string nextCursorMark)
{
if (Request.IsAjaxRequest())
{
var page = GetPagedArticles(15, "*:*", nextCursorMark);
return PartialView("_ArticlesPartial", page);
}
else
{
var page = GetPagedArticles(30, "*:*");
return View(page);
}
}
page
contains the string which I need to pass on. When the View is rendered, the value is added from that view by javascript, which means that the next request (the partial) is of course fetched correctly. The problem is that all subsequent requests are returning the same value, as I can't figure out how to pass on the value (either using MVC or javascript).
The script added to the View (most code omitted):
@section scripts {
<script type="text/javascript">
$(window).scroll(function () {
if ($(window).scrollTop() == $(document).height() - $(window).height() && !hasData && !inProgress) {
$.ajax("@Url.Action("Index", "Articles", new { nextCursorMark = Model.CurserMark })", {
...
}});
</script>
}
Upvotes: 0
Views: 701
Reputation:
Razor is parsed on the server before it is sent to the view so
$.ajax("@Url.Action("Index", "Articles", new { nextCursorMark = Model.CurserMark })"
is evaluating to the url to '/Articles/Index/#
where # is the initial value of CursorMark
when the page is first loaded.
You need to include a variable that you can increment with each call
var cursor = '@Model.CursorMark'; // initial value
....
$.ajax('@Url.Action("Index", "Articles")', { newCursorMark: cursor } {
// update the DOM
....
cursor += someIncrement; // increment the value for the next call
}});
Upvotes: 1