MVC Surface Controller & Umbraco Current Node

14,336

Solution 1

I am using Umbraco 6.0.4 and it is as simple as:

var currentNode = Umbraco.TypedContent(UmbracoContext.PageId);

Solution 2

Same problem I have with getting Current Node Id in Surface Controller while Ajax Post, when we are loosing hidden value of uformpostroutevals.

Even if I'm trying to post this value, by taking it from form, rendered by

@using (Html.BeginUmbracoForm("<ActionName>", "<Controller Name>Surface"))

I Still have null in all properties of UmbracoContext, so looks like it is not initialized properly.

HOTFIX: I'm passing CurrentNodeId to each form which I'm sending by by Ajax:

In General master page I'm creating global javascript object:

<script type="text/javascript">
  var Global = {
    //..List of another variables which can be usefull of frontend
    currentNodeId: @CurrentPage.Id
  };
</script>

In any request it is easy to use Global.currentNodeId as one of params of data:

var sendData = {
    currentNodeId: Global.currentNodeId,
    // another params 
};

$.ajax({
    method: 'POST',
    data: JSON.stringify(sendData),
    contentType: 'application/json; charset=utf-8',
    url: '/Umbraco/Surface/<ControllerName>Surface/<ActionName>',
    dataType: 'json',
    cache: false
})

Please pay attention that it is only a hot fix and not a proper solution!

Share:
14,336
user1288337
Author by

user1288337

Updated on June 05, 2022

Comments

  • user1288337
    user1288337 almost 2 years

    I'm trying to write a childaction function in a surface controller that gets called by a macro to render a PartialView.

    I need in this function to gain access to my current page properties to then tweak the rendered PartialView with.

    I got this from Jorge Lusar's code on ubootstrap and it works fine on the HttpPost ActionResult function :

    var renderModel = (UmbracoRenderModel)ControllerContext.RouteData.DataTokens["umbraco"];
    var currentPage = renderModel.CurrentNode.AsDynamic();
    

    Problem is I've this error thrown on [ChildActionOnly] PartialViewResult function :

    Unable to cast object of type 'System.String' to type 'Umbraco.Cms.Web.Model.UmbracoRenderModel'.
    on 'var renderModel = (UmbracoRenderModel)ControllerContext.RouteData.DataTokens["umbraco"];'
    

    Data in DataTokens["umbraco"] seems to change between the two functions. If I diplay DataTokens["umbraco"].ToString() on each one, here is what happens:

    On [ChildActionOnly] public PartialViewResult Init() -> "Surface" is displayed.

    On [HttpPort] public HandleSubmit(myModel model) -> "Umbraco.Cms.Web.Model.UmbracoRenderModel" is displayed.

    Thanks for any advice here.

    Nicolas.