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}.
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.
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.
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.
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 :)
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.
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.
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
Anyone ?
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.
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
Hi Shannon,
I'm having a problem using dictionary items in a surface controller action when called by ajax.
In my view I use dictionary items.
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:
Any idea how to solve this.
Dave
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 :)
I think the problem the current culture is not set when a action on a surfacecontroller is called directly
Dave
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
I had this same problem and retrieving the dictionary items using
Umbraco.GetDictionaryValue
instead ofUmbraco.Field
did the trick for me as well. You just saved me a lot of hours. Thank you so much.glad you managed to solve it. Can i ask why you have that logic in your controller? What is it doing? Charlie :)
Hi Charles,
I the code in my controller get's data from umbraco and a webservice and creates a list of custom models.
Dave
Ahhh i see, thanks :)
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!
is working on a reply...