MVC Surface Controller & Umbraco Current Node

I am trying to write a childaction function in a surface controller that is called by a macro to render a PartialView.

I need this function in order to access my current page properties, then to customize the rendering of PartialView with.

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

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

The problem is that this error occurred while using the [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"];'

The data in DataTokens ["umbraco"] seems to change between the two functions. If I am diplay DataTokens ["umbraco"]. ToString () for each of them, this is what happens:

In [ChildActionOnly], PartialViewResult Init () → "Surface" is displayed .

In [HttpPort] displayed publicly available HandleSubmit (model MyModel) → "Umbraco.Cms.Web.Model.UmbracoRenderModel" .

Thanks for any advice here.

Nicolas.

+3
source share
7 answers

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

var currentNode = Umbraco.TypedContent(UmbracoContext.PageId);
+5
source

The same problems that I encountered getting Current Node Id in Surface Controller during Ajax Post, when we lose the hidden value uformpostroutevals.

Even if I try to post this value by taking it from the form displayed

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

null UmbracoContext, , .

HOTFIX. CurrentNodeId , Ajax:

javascript:

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

Global.currentNodeId 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
})

, , !

+2

,

public class CommentSurfaceController : SurfaceController
{
    private readonly IUmbracoApplicationContext context;
    public CommentSurfaceController(IUmbracoApplicationContext context)
    {
        this.context = context;
    }
}

umbracos, , .

SurfaceController https://github.com/umbraco/Umbraco5Docs/blob/5.1.0/Documentation/Getting-Started/Creating-a-surface-controller.md

0

, , . , , ( HTTP GET) ( HTTP POST).

, CurrentContent, .

using System.Web.Mvc;
using Umbraco.Cms.Web;
using Umbraco.Cms.Web.Surface;
using Umbraco.Cms.Web.Model;

namespace Whatever
{
    public abstract class BaseSurfaceController : SurfaceController
    {
        private object m_currentContent = null;

        public dynamic CurrentContent
        {
            get
            {
                if (m_currentContent == null)
                {
                    if (Request.HttpMethod == "POST")
                    {
                        m_currentContent = GetContentForSubmitAction();
                    }
                    else
                    {
                        m_currentContent = GetContentForChildAction();
                    }
                }
                return m_currentContent;
            }
        }

        // from Lee Gunn response
        // http://our.umbraco.org/forum/core/umbraco-5-general-discussion/29178-In-a-controller-how-do-I-get-the-current-pages-hiveId?p=2

        private object GetContentForChildAction()
        {
            ViewContext vc = ControllerContext.RouteData.DataTokens[
                    "ParentActionViewContext"] as ViewContext;
            var content = vc.ViewData.Model
                    as global::Umbraco.Cms.Web.Model.Content;
            return content.AsDynamic();
        }

        // from Nicholas Ruiz
        // http://our.umbraco.org/forum/core/umbraco-5-general-discussion/30928-Surface-Controller-and-Current-Node-Properties

        private object GetContentForSubmitAction()
        {
            UmbracoRenderModel rm =
                    ControllerContext.RouteData.DataTokens["umbraco"] as UmbracoRenderModel;
            if (rm == null)
            {
                return GetContentForChildAction();
            }
            return rm.CurrentNode.AsDynamic();
        }
    }
}

, , .

0

Umbraco 7.2.1 , ChildActionOnly .

        [ChildActionOnly]
    public ActionResult InitializeDataJson(KBMasterModel model)
    {
        var pluginUrl = string.Concat("/App_Plugins/", KBApplicationCore.PackageManifest.FolderName);
        bool isAuthenticated = Request.IsAuthenticated;
        IMember member = null;
        if (isAuthenticated)
            member = UmbracoContext.Application.Services.MemberService.GetByUsername(User.Identity.Name);
        var data = new { CurrentNode = model.IContent, IsAuthenticated = isAuthenticated, LogedOnMember = member, PluginUrl = pluginUrl };
        var json = JsonConvert.SerializeObject(data, Formatting.None, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        var kbData = new TLCKBData() { InitializationJson = json };
        return PartialView(kbData);
    }

:

@model TLCKBData
<script>
    (function () {
        var data = JSON.parse('@Html.Raw(Model.InitializationJson)');
        tlckb.init(data);
    })();
</script>

, :

@section FooterScript {
    @{Html.RenderAction("InitializeDataJson", "KBPartialSurface", new { model = Model });}
}

. , , , , , UmbracoTemplatePage ( Umbraco), , RenderModel UmbracoTemplatePage.

.

, , , . GetContent .

, , - , API angular. , , .. , .. ..

, , . , "", ? , , .

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

,

var currentNode = CurrentPage;

( )

 //
 // Summary:
 //     Gets the current page.
 protected virtual IPublishedContent CurrentPage { get; }

, null, , currentPage.

0

.

jquery , .

    <script type="text/javascript">
        $.ajaxSetup({
            headers: { 'umbraco-page-id': '@CurrentPage.Id' }
        });
    </script> 
Hide result

jquery umbraco. .

0

All Articles