Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Mar 14, 2013 @ 13:13
    Dave Woestenborghs
    1

    Surfacecontroller and ajax request to action

    Hi,

    I build a surfacecontroller with a action for fetching data from umbraco documents and combining it with custom data from a webservice and returns a view with a custom model. The action has one optional parameter.

        public class MyController : SurfaceController
        {
    
            public ActionResult MyAction(string myparameter)
            {
                var nodes = this.Umbraco.AssignedContentItem.Descendants("myDoctypes");
    
               //do somestuff with the nodes here to create my custom model
    
                return PartialView("MyView", myModel);  
        }
    }

    When I call the action from a template everything works fine. But when I try to call the same action from ajax or the browser (e.g. http://mydomain/mycontroller/myaction/myparameter) like I would do with regular MVC I get a error and my action is never called.

    Looking at the documentation I see all request get routed to umbraco/surface/{controller}/{action}/{id}.

    So when I call http://mydomain/umbraco/surface/mycontroller/myaction/myparameter my action is called but I can't get the correct Umbraco Context, so I can't access my Umbraco content. 

    How should this be done ?

    Dave

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Mar 15, 2013 @ 10:49
    Dave Woestenborghs
    0

    Anyone ?

  • Shannon Deminick 1524 posts 5269 karma points MVP 2x
    Mar 15, 2013 @ 18:20
    Shannon Deminick
    0

    What do you mean by the 'correct umbraco context' ?

    The request you are making is completely outside of the Umbraco process, this is simply just a regular Http request so there is no AssignedContentItem.

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Mar 15, 2013 @ 20:12
    Dave Woestenborghs
    0

    I was just wondering what 'best practice' for Ajax request would be in Umbraco. I already solved it by creating a second action which takes the parameter of the node calling the action.

    So problem solved.

    Dave

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Mar 22, 2013 @ 10:11
    Dave Woestenborghs
    0

    Hi Shannon,

    I'm having a problem using dictionary items in a surface controller action when called by ajax.

    public ActionResult NewsList(int id)
            {
                UmbracoHelper helper = new UmbracoHelper(this.UmbracoContext);
    
    
                IPublishedContent content = helper.TypedContent(id);
                var newsNodes = content.AncestorOrSelf(1).Descendants("News");
    
                List<News> newslist = //code to get some extra data
    
                return PartialView("NewsListSmall", model: newslist.OrderByDescending(n => n.Date).ToList());
            }

     

    In my view I use dictionary items.

    @Umbraco.Field("#More")

    When I call the action from a template everything works fine.

    If I call the action from a ajax request without using dictionary items it works fine too. But when I call it from a ajax request and try to access dictionary items it fails. I get the following error on the code of the dictionary item:

    Cannot return the IPublishedContent because the UmbracoHelper was constructed with an UmbracoContext and the current request is not a front-end request.

    Any idea how to solve this.

    Dave

  • Charles Afford 1163 posts 1709 karma points
    Mar 22, 2013 @ 13:22
    Charles Afford
    0

    Hi, i had the same problem.  I think.  There is a fix coming for this that allows you to be able to get the context using ajax.  What i ended up doing was setting my node id property in my custom model in my view (Where you can get the currentId).  :).  Then i can get the node by id in the controller using the node id i set in my view.  Hope this helps :)

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Mar 22, 2013 @ 13:30
    Dave Woestenborghs
    0

    I think the problem the current culture is not set when a action on a surfacecontroller is called directly 

     

    Dave

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Mar 22, 2013 @ 16:50
    Dave Woestenborghs
    1

    I found a solution for my problem.

    Instead of using @Umbraco.Field syntax to display dictionary items, I now use @Umbraco.GetDictionaryValue("More") and that seems to work just fine.

    Dave

  • Mark Bowser 273 posts 860 karma points c-trib
    Feb 19, 2016 @ 21:30
    Mark Bowser
    0

    I had this same problem and retrieving the dictionary items using Umbraco.GetDictionaryValue instead of Umbraco.Field did the trick for me as well. You just saved me a lot of hours. Thank you so much.

  • Charles Afford 1163 posts 1709 karma points
    Mar 23, 2013 @ 14:09
    Charles Afford
    0

    glad you managed to solve it.  Can i ask why you have that logic in your controller?  What is it doing?  Charlie :)

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Mar 25, 2013 @ 10:56
    Dave Woestenborghs
    0

    Hi Charles,

    I the code in my controller get's data from umbraco and a webservice and creates a list of custom models. 

    Dave

  • Charles Afford 1163 posts 1709 karma points
    Mar 25, 2013 @ 22:45
    Charles Afford
    0

    Ahhh i see, thanks :)

  • Mark 255 posts 612 karma points
    Oct 28, 2013 @ 10:57
    Mark
    0

    As this is a similar thread and all the key people are watching it. I'm having an issue specified: http://our.umbraco.org/forum/developers/api-questions/45776-Calling-Surface-Controller-From-Ajax. I'd really appreciate it if someone could have a quick look. Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft