Hey while trying to create a newspage I wanted to create a button that retrieved more news articles using ajax to call a partial view macro that updated a part of my page. At the moment this is not working however and I was hoping someone could give me some pointers as to how I can get it working.
On my news page I have the following:
<button class ="myClass" onclick="ShowMoreNewsItems()">show more</button>
Which calls the following in the header:
<script>
function ShowMoreNewsItems()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","Umbraco.RenderMacro(\"GetSmallNews\", new {number=\"0\"})",true);
xmlhttp.send();
}
</script>
Any help would be greatly appreciated.
Note that I'm fairly new to Umbraco so if there is a better way to do this I'm also open to suggestions
It routes me to .../umbraco/Surface/NewsSurface/getNews and gives:
HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Which makes me think that the controller is visible to it but the method is not (as when I type a different controller name that does not exist it just refreshes the page without doing anything else).
In the controller class I have not changed anything except add [UmbracoAuthorize] above the method and tried to return some diferent values (which did not make any difference).
I have also tried adding" , new { @class = "btn btn-primary" })" to the code above but then I got the following error:
There is not current PublishedContentRequest, it must be initialized before the RenderRouteHandler executes.
This seems to be a step backwards as it will now always give this error regardless of whatever I try to make a call to an actual controller or to a non existing one.
Any suggestions are welcome and greatly appreciated.
Did you add the reference for the javascript library @Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js") you are getting the url error becuase its not actually making the ajax call so would lead me to believe you do not have the ajax library required.
Note that do add this I did use nuget to install the System.Web.Optimization package so it seemed I was indeed missing the library but there is something else also going wrong.
ajax with partial view macro
Hey while trying to create a newspage I wanted to create a button that retrieved more news articles using ajax to call a partial view macro that updated a part of my page. At the moment this is not working however and I was hoping someone could give me some pointers as to how I can get it working. On my news page I have the following:
Which calls the following in the header:
Any help would be greatly appreciated. Note that I'm fairly new to Umbraco so if there is a better way to do this I'm also open to suggestions
Hi Roald,
Try to use some apiController method, not Umbraco.RenderMacro in js.
Roald,Alex,
If doing umbraco with mvc you can also create ajax link and make ajax calls to get data eg
And the controller
More information about ajax link here http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/ajax-actionlink-and-html-actionlink-in-mvc/
Regards
Ismail
Hey I still can't quite get it to work, I have created a class NewsServiceController in a new folder named news.
This is my controller:
I than tried to aces it in 2 different ways (both which are not working).
The first way was by using the javascript I already had:
I tried this both with and without the using part neither seemed to do anything, when inspecting the html of the page I saw that
was changed to
Which seemd ok to me.
I also tried it using the actionlink for which the code looked as follows:
This didn't work either it instead redirects me to .../umbraco/Surface/NewsSurface/getNews().
It would be greatly appreciated if you guys could tell me what I am missing.
Get rid of the form stuff you do not need it see my example no form in sight.
Even without the form stuff I can't get it to work, when I have:
It routes me to .../umbraco/Surface/NewsSurface/getNews and gives:
HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Which makes me think that the controller is visible to it but the method is not (as when I type a different controller name that does not exist it just refreshes the page without doing anything else).
In the controller class I have not changed anything except add [UmbracoAuthorize] above the method and tried to return some diferent values (which did not make any difference).
I have also tried adding" , new { @class = "btn btn-primary" })" to the code above but then I got the following error:
There is not current PublishedContentRequest, it must be initialized before the RenderRouteHandler executes.
This seems to be a step backwards as it will now always give this error regardless of whatever I try to make a call to an actual controller or to a non existing one.
Any suggestions are welcome and greatly appreciated.
Roald,
Did you add the reference for the javascript library @Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js") you are getting the url error becuase its not actually making the ajax call so would lead me to believe you do not have the ajax library required.
Regards
Ismail
Hello,
Another option is to use alt templates with macro's or partial views. In that case you don't need to write your custom ajax calls so I think it's much easier. More info here: http://24days.in/umbraco/2012/ajax-paging-using-alttemplates/
Jeroen
After adding:
the end result is still the same.
Note that do add this I did use nuget to install the System.Web.Optimization package so it seemed I was indeed missing the library but there is something else also going wrong.
Any ideas?
is working on a reply...