I'm missing something simple and just can't seem to put my finger on it. (Umbraco v8.13.0)
I've got a partial view with a custom model and this view is shared/rendered in different areas of the site. This view has some functionality (buttons) that I may or may not want to display depending on where the view is rendered. Seems like I should be able to say if its node id 111, 222, then true else false kind of thing. But I just can't seem to get the node id for the life of me.
In the view, I've tried changing the @model MVC directive to:
@inherits UmbracoViewPage
Then I tried it on the Controller side to just dump the id into the model I'm sending to the view trying:
int myId = Umbraco.Web.Composing.Current.UmbracoHelper.AssignedContentItem.Id;
...and...
Typically my error is: Cannot return the IPublishedContent because the UmbracoHelper was not constructed with an IPublishedContent. I am using VS and the auto-complete indicates that its finding the references fine.
I just feel like fighting this where it's just something simple that I'm missing. Maybe a constructor or using statement?
Without seeing a bit more of your code it's a little difficult to see what's happening here.
Is your controller inheriting from Umbraco.Web.Mvc.SurfaceController? if so, then you should be able to access the UmbracoHelper natively just like this:
Umbraco.AssignedContentItem.Id
Likewise, if your view is an UmbracoViewPage you should also have a handle to the context of the Umbraco page being rendered so the same code should work there too.
Feel free to post more of your code in its entirety as that should help determine what might be preventing these helpers from becoming available.
I get a 500 error on the AssignedContentItem.Id line stating the following:
Cannot return the IPublishedContent because the UmbracoHelper was not
constructed with an IPublishedContent.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Cannot return the
IPublishedContent because the UmbracoHelper was not constructed with
an IPublishedContent.
Without the AssignedContentItem line the view renders without any issues. I'll try again on the Controller side and post what I run into there.
That's likely the issue and there is a piece of the puzzle that I'm just missing.
On the page(s), there are two panes of data being displayed. The top pane is a summary table. The user clicks on a row in that table which triggers an ajax call to my controller to fetch the details. On the controller side, I'm making a call to my SQL database and returning a model with the data.
Maybe I can't do it on the view side and need to do it on the Controller side which is fine, but I can't get it to work there either. I feel like I'm fighting it which is typically an indication that I'm doing it wrong and just missing something.
Thanks in advance for any insight you can provide.
Yes, what is going wrong here is the fact that an API controller has no idea about the current state of the user. So it doesn't know what page the request came from, what language etc... Hence why the error states that it has no IPublishedContent.
So in this case, you have to pass something that allows you to get the current page. The easiest way to do this is to pass the id of the current page to the API controller and then use that to get the page.
Thank you. Not the solution that I was hoping for, but it does resolve my issue.
For anyone else coming across this, I just added a data attribute to the page title ([email protected]), then before my ajax call I grab it ($('h2').first().data('nodeid')) and pass it over to the Controller as another parameter.
It does mean that I have to update all of the models to send the value back with the data, but it is what it is.
Umbraco v8 - Getting Current Node Id
Hey All,
I'm missing something simple and just can't seem to put my finger on it. (Umbraco v8.13.0)
I've got a partial view with a custom model and this view is shared/rendered in different areas of the site. This view has some functionality (buttons) that I may or may not want to display depending on where the view is rendered. Seems like I should be able to say if its node id 111, 222, then true else false kind of thing. But I just can't seem to get the node id for the life of me.
In the view, I've tried changing the @model MVC directive to: @inherits UmbracoViewPage
Then I tried it on the Controller side to just dump the id into the model I'm sending to the view trying:
int myId = Umbraco.Web.Composing.Current.UmbracoHelper.AssignedContentItem.Id; ...and...
IPublishedContent nodeInfo = Umbraco.AssignedContentItem; results.myId = nodeInfo.Id;
Typically my error is: Cannot return the IPublishedContent because the UmbracoHelper was not constructed with an IPublishedContent. I am using VS and the auto-complete indicates that its finding the references fine.
I just feel like fighting this where it's just something simple that I'm missing. Maybe a constructor or using statement?
Thank you in advance, Rick
Without seeing a bit more of your code it's a little difficult to see what's happening here.
Is your controller inheriting from Umbraco.Web.Mvc.SurfaceController? if so, then you should be able to access the UmbracoHelper natively just like this:
Likewise, if your view is an UmbracoViewPage you should also have a handle to the context of the Umbraco page being rendered so the same code should work there too.
Feel free to post more of your code in its entirety as that should help determine what might be preventing these helpers from becoming available.
Chris,
Thank you for the quick reply.
Below is a snippet from the Partial View and the resulting error message. Let me know if you see anything obvious that I'm missing.
I get a 500 error on the AssignedContentItem.Id line stating the following:
Without the AssignedContentItem line the view renders without any issues. I'll try again on the Controller side and post what I run into there.
Thank you again, Rick
Hi Rick,
How do you render your Partial? It seems like it's rendered outside of the Umbraco context (hence why it's constructed without a published content)
Patrick,
That's likely the issue and there is a piece of the puzzle that I'm just missing.
On the page(s), there are two panes of data being displayed. The top pane is a summary table. The user clicks on a row in that table which triggers an ajax call to my controller to fetch the details. On the controller side, I'm making a call to my SQL database and returning a model with the data.
Controller Snippet:
Maybe I can't do it on the view side and need to do it on the Controller side which is fine, but I can't get it to work there either. I feel like I'm fighting it which is typically an indication that I'm doing it wrong and just missing something.
Thanks in advance for any insight you can provide.
Rick
Yes, what is going wrong here is the fact that an API controller has no idea about the current state of the user. So it doesn't know what page the request came from, what language etc... Hence why the error states that it has no IPublishedContent.
So in this case, you have to pass something that allows you to get the current page. The easiest way to do this is to pass the id of the current page to the API controller and then use that to get the page.
Patrick,
Thank you. Not the solution that I was hoping for, but it does resolve my issue.
For anyone else coming across this, I just added a data attribute to the page title ([email protected]), then before my ajax call I grab it ($('h2').first().data('nodeid')) and pass it over to the Controller as another parameter.
It does mean that I have to update all of the models to send the value back with the data, but it is what it is.
Thanks again for the assistance, Rick
is working on a reply...