Current node Id, Url.Action, PagedList and partial view (when I want view)
When I click on my X.PagedList pager, and in my surfacecontroller, I've got this message at the same line I,ve used Node.getCurrentNodeId(): System.InvalidOperationException: There is no current node.
When I click on my X.PagedList pager, and in my surfacecontroller, I've got this message at the same line I,ve used var currentNode = UmbracoContext.Current.PublishedContentRequest.PublishedContent): System.NullReferenceException: Object reference not set to an instance of an object..
It's the links in @Html.PagedListPager.. I cant find current node id. All paging links actally working fine in one page, but not in their child pages (where the node id error messages appears).
As I see it that post, you are asking server with jQuery method - then you need to pass current page id, AJAX requests haven't Umbraco CurrentPage, you have to instantiate it.
I don't have much Ajax experience yet, so I'm not sure how you mean. Do you have an example to me? I don't have access to my websolution now, so I will try again tomorrow.
What Alex mentioned is right, when you reach out to a surface controller using jQuery the server does not have the context of the current page id and will always return a null. You have to pass the current node id while calling the surface controller . You can pass the node id of the current page to the surface controller as an action and you can then calculate the node from there on. I have a UmbracoHelperWrapper class which I use to dependency inject and I use that in my services to get the TypedContent from the node id.
How is UmbracoHelperWrapper used? Is it available to everyone?
I now find nodeId with UmbracoContext.PageId.GetValueOrDefault() from view (the others didn't work), and now pass my current nodeId with this Url.Action in pager inside from my partial view:
@Html.PagedListPager((IPagedList)ViewBag.pageList,
page => Url.Action("HikingDestinationGroupedByVisitor",
new { page, nodeId = UmbracoContext.PageId.GetValueOrDefault(), PagedListRenderOptions.OnlyShowFivePagesAtATime }),
new PagedListRenderOptions { Display = PagedListDisplayMode.IfNeeded })
Into my partial macro view with this (I had to use UmbracoContext.PageId.GetValueOrDefault() also here, so somethings not quite right):
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@Html.Action("HikingDestinationGroupedByVisitor", "HikingDestinationSurface", new { nodeId = @UmbracoContext.PageId.GetValueOrDefault() })
and it's find the current node Id inside this method in surfacecontroller: public PartialViewResult HikingDestinationGroupedByVisitor(int? page, int nodeId). where I can use nodeId in my method, but now there is no difference with or without AJAX in the partial view.
The problem is that page1 in this partial view still is OK from load, but the next page I choose from pager ONLY just show the partial view on screen, and not the whole page (and then of cource I miss nodeId after the next page again).
This partial view only that appears alone with the action button has long been a big challenge, so someone knows how to solve this?
Or anyone who can show me how to use this AJAX method instead of if it's best suited for the purpose of opening the entire page, along with the value of current node id (in conjunction with the pager)?
<script>
$(function () {
$('#TrHikingDestinationGroupedByVisitor').on('click', 'td ul li a', function () {
$.ajax({
url: this.href,
//data: "nodeId=" + @UmbracoContext.PageId.GetValueOrDefault(),
type: 'GET',
cache: false,
success: function (result) {
$('#DivHikingDestinationGroupedByVisitor').html(result);
}
});
return false;
});
});
</script>
Still unsolved. Someone else who has another universal MVC pager solution for Umbraco 7.7.3 (Surfacecontroller), with no use og jQuery/AJAX?
Many examples, but they all return View - not PartialView from surfacecontroller (which my pages consist of, and then there is more then one pager on the same page), so they can't have same url's, but then I may have to use jQuery / AJAX anyway? (still not resolved how to transfer current node id to surfacecontroller using Jquery / AJAX)
Thank you, I will try your solution this weekend, or maybe after the weekend. Do you know if there can be more then one pager on the same page (several partial views with several queries in same page), or do I need a Jquery / AJAX solution then?
I'm not quite sure how you mean, since it's the paginglinks in my partial views that's the challenge now. I've found some pagers that work if I don't need to transfer current node id to the surfacecontroller (like X.PagedList.Mvc nuget package).
I've also tested this solution and received the correct database results with correct number of pagingbuttons in my partial views, but it fails when I try out the urls in pagerlinks, and try to transfer a value of current node Id with use of Jquery / AJAX (I've tried to replace the pagerlinks in view in this example since they probably will not work if more pager than one exists in the same page): http://jasonwatmore.com/post/2015/10/30/aspnet-mvc-pagination-example-with-logic-like-google
Very interesting example of Paul Seal I would have liked to implement later, but I'm not sure if lucene search will work in my custom table using petapoco?
So meanwhile: someone who knows how to instantiate and transfer values from (partial) view to surfacecontroller, such as current node id with use of Jquery/AJAX and how the url buttonlinks must be?
<script>
$(function () {
$('#TrHikingDestinationGroupedByVisitor').on('click', 'td ul li a', function () {
$.ajax({
url: this.href,
//data: "nodeId=" + @UmbracoContext.PageId.GetValueOrDefault(),
type: 'GET',
cache: false,
success: function (result) {
$('#DivHikingDestinationGroupedByVisitor').html(result);
}
});
return false;
});
});
</script>
And how do the pagerlinks have to be built? Url.Action, Url.SurfaceAction, Html.Action, or < a hfer data-ajax="true" data-ajax-method="GET" data-ajax-mode="replace" - something like this?
It seems like I'm almost in goal, so I'd been very grateful for examples on this at the moment.
I had trouble activating jquery in some cases, but now I have submitted a test that shows when jquery works.
The jquery function now works if I replace nodeId (curent node id) with id (id: id or data: {id: id} or data: json.stringify(id)) but for every new page i now opens with a paging button, but only partial view opens and jquery / ajax will be disabled.
$(document).on("click", "#TrLastVisitorsHikingDestination td ul li a", function() {
$.ajax({
url: this.href,
type: 'GET',
data: { id: id },
cache: false,
success: function (data) {
$('#DivLastVisitorsHikingDestination').html(result);
},
error: function (data) {
alert("No success (Jquery/Ajax still activated)");
}
});
return false;
});
Do I have to switch from GET to POST, and create a custom method for JsonResult in the surface controller? Anyone who knows how this is solved, or who has a complete MVC example of paging with ajaxbuttons?
Current node Id, Url.Action, PagedList and partial view (when I want view)
When I click on my X.PagedList pager, and in my surfacecontroller, I've got this message at the same line I,ve used
Node.getCurrentNodeId()
: System.InvalidOperationException: There is no current node.When I click on my X.PagedList pager, and in my surfacecontroller, I've got this message at the same line I,ve used
var currentNode = UmbracoContext.Current.PublishedContentRequest.PublishedContent)
: System.NullReferenceException: Object reference not set to an instance of an object..Why does this happen and how can I find node Id?
Hi Tom
What version of Umbraco are you using?
Is it ajax request to surfaceController?
Thanks,
Alex
Hi Alex, I'm using ver 7.7.3 now, and I'm using same as in this link: https://our.umbraco.org/forum/extending-umbraco-and-using-the-api/88538-xpagedlist-from-view-to-partial-view
It's the links in
@Html.PagedListPager..
I cant find current node id. All paging links actally working fine in one page, but not in their child pages (where the node id error messages appears).As I see it that post, you are asking server with jQuery method - then you need to pass current page id, AJAX requests haven't Umbraco CurrentPage, you have to instantiate it.
Thanks,
Alex
I don't have much Ajax experience yet, so I'm not sure how you mean. Do you have an example to me? I don't have access to my websolution now, so I will try again tomorrow.
What Alex mentioned is right, when you reach out to a surface controller using jQuery the server does not have the context of the current page id and will always return a null. You have to pass the current node id while calling the surface controller . You can pass the node id of the current page to the surface controller as an action and you can then calculate the node from there on. I have a UmbracoHelperWrapper class which I use to dependency inject and I use that in my services to get the TypedContent from the node id.
How is UmbracoHelperWrapper used? Is it available to everyone?
I now find nodeId with
UmbracoContext.PageId.GetValueOrDefault()
from view (the others didn't work), and now pass my current nodeId with thisUrl.Action
in pager inside from my partial view:Into my partial macro view with this (I had to use
UmbracoContext.PageId.GetValueOrDefault()
also here, so somethings not quite right):and it's find the current node Id inside this method in surfacecontroller:
public PartialViewResult HikingDestinationGroupedByVisitor(int? page, int nodeId)
. where I can use nodeId in my method, but now there is no difference with or without AJAX in the partial view.The problem is that page1 in this partial view still is OK from load, but the next page I choose from pager ONLY just show the partial view on screen, and not the whole page (and then of cource I miss nodeId after the next page again).
This partial view only that appears alone with the action button has long been a big challenge, so someone knows how to solve this?
Or anyone who can show me how to use this AJAX method instead of if it's best suited for the purpose of opening the entire page, along with the value of current node id (in conjunction with the pager)?
Still unsolved. Someone else who has another universal MVC pager solution for Umbraco 7.7.3 (Surfacecontroller), with no use og jQuery/AJAX?
Many examples, but they all return View - not PartialView from surfacecontroller (which my pages consist of, and then there is more then one pager on the same page), so they can't have same url's, but then I may have to use jQuery / AJAX anyway? (still not resolved how to transfer current node id to surfacecontroller using Jquery / AJAX)
I developed my own paging. If help:
https://gist.github.com/marciogoularte/a4408b7b79c681dcbe7bf6fadc9f2fbf
Only adapt to your html.
is it possible to put the template logic a RenderMvcController
This is part of code that I used on my blog. I will soon be available as a package
UPDATE: I'll create a complete example using RenderMvcController and I'll let you know here
Thank you, I will try your solution this weekend, or maybe after the weekend. Do you know if there can be more then one pager on the same page (several partial views with several queries in same page), or do I need a Jquery / AJAX solution then?
You can use my example as a guide to create a SurfaceController. Creating a viewmodel with different properties of type Pager.
Another good example is this that Paul Seal developed.
http://www.codeshare.co.uk/blog/how-to-search-by-document-type-and-property-in-umbraco/
I'm not quite sure how you mean, since it's the paginglinks in my partial views that's the challenge now. I've found some pagers that work if I don't need to transfer current node id to the surfacecontroller (like X.PagedList.Mvc nuget package).
I've also tested this solution and received the correct database results with correct number of pagingbuttons in my partial views, but it fails when I try out the urls in pagerlinks, and try to transfer a value of current node Id with use of Jquery / AJAX (I've tried to replace the pagerlinks in view in this example since they probably will not work if more pager than one exists in the same page): http://jasonwatmore.com/post/2015/10/30/aspnet-mvc-pagination-example-with-logic-like-google
Very interesting example of Paul Seal I would have liked to implement later, but I'm not sure if lucene search will work in my custom table using petapoco?
So meanwhile: someone who knows how to instantiate and transfer values from (partial) view to surfacecontroller, such as current node id with use of Jquery/AJAX and how the url buttonlinks must be?
And how do the pagerlinks have to be built? Url.Action, Url.SurfaceAction, Html.Action, or < a hfer data-ajax="true" data-ajax-method="GET" data-ajax-mode="replace" - something like this?
It seems like I'm almost in goal, so I'd been very grateful for examples on this at the moment.
I had trouble activating jquery in some cases, but now I have submitted a test that shows when jquery works.
The jquery function now works if I replace nodeId (curent node id) with id (
id: id
ordata: {id: id
} ordata: json.stringify(id)
) but for every new page i now opens with a paging button, but only partial view opens and jquery / ajax will be disabled.Do I have to switch from GET to POST, and create a custom method for JsonResult in the surface controller? Anyone who knows how this is solved, or who has a complete MVC example of paging with ajaxbuttons?
Do I need to divide up what is returned from the controller?
is working on a reply...