Copied to clipboard

Flag this post as spam?

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


  • Roald 6 posts 25 karma points
    Sep 12, 2014 @ 10:28
    Roald
    0

    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:

    <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

  • Alex Skrypnyk 6148 posts 24077 karma points MVP 8x admin c-trib
    Sep 12, 2014 @ 12:49
    Alex Skrypnyk
    0

    Hi Roald,

    Try to use some apiController method, not Umbraco.RenderMacro in js.

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Sep 12, 2014 @ 14:01
    Ismail Mayat
    0

    Roald,Alex,

    If doing umbraco with mvc you can also create ajax link and make ajax calls to get data eg

            @Ajax.ActionLink(approvalButtonText, "ModerateReview", "AdminReviewSurface", new { reviewId = Model.Id, approve = !Model.Approved },
                        new AjaxOptions
                        {
                            UpdateTargetId = "review-" + @Model.Id,
                            InsertionMode = InsertionMode.Replace,
                            LoadingElementId = "loading",
                            HttpMethod = "GET"
                        }, new { @class = "btn btn-primary" })

    And the controller

            [HttpGet]
            [UmbracoAuthorize]
            public ActionResult ModerateReview(int reviewId, bool approve)
            {
                var review = _reviewService.ModerateReview(reviewId, approve);
                if (review != null && review.Id != null && review.Id > 0)
                {
                  return PartialView("~/Views/Partials/Admin/Reviews/ReviewData.cshtml", review);
                }
    
                var errorReview = new ProductReview {Comment = "Error", FirstName = "Error", LastName = "Error"};
                return PartialView("~/Views/Partials/Admin/Reviews/ReviewData.cshtml", errorReview);
            }

     

    More information about ajax link here http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/ajax-actionlink-and-html-actionlink-in-mvc/

    Regards

     

    Ismail

     

  • Roald 6 posts 25 karma points
    Sep 12, 2014 @ 15:51
    Roald
    0

    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:

    public class NewsSurfaceController : Umbraco.Web.Mvc.SurfaceController
    {
    
    [HttpGet]
    public ActionResult getNews()
    {
        return CurrentUmbracoPage();
    }
    }
    

    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:

    <script>
        using (Html.BeginUmbracoForm("News", "NewsSurface"))
        {
            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", '@Url.Action("getNews()", "NewsSurface")', true);
                xmlhttp.send();
            }
        }
    </script>
    

    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

    xmlhttp.open("GET", '@Url.Action("getNews()", "NewsSurface")', true);
    

    was changed to

                xmlhttp.open("GET", '/umbraco/Surface/NewsSurface/getNews()', true);
    

    Which seemd ok to me.

    I also tried it using the actionlink for which the code looked as follows:

            @using (Html.BeginUmbracoForm("News", "NewsSurface"))
            {
            @Ajax.ActionLink("test", "getNews()", "NewsSurface", new AjaxOptions
                    {
                        UpdateTargetId = "myDiv",
                        InsertionMode = InsertionMode.Replace,
                        LoadingElementId = "loading",
                        HttpMethod = "GET"
                    })
            }
    

    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.

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Sep 13, 2014 @ 11:21
    Ismail Mayat
    0

    Get rid of the form stuff you do not need it see my example no form in sight.

  • Roald 6 posts 25 karma points
    Sep 15, 2014 @ 09:48
    Roald
    0

    Even without the form stuff I can't get it to work, when I have:

    @Ajax.ActionLink("test", "getNews", "NewsSurface", new AjaxOptions
                        {
                            UpdateTargetId = "myDiv",
                            InsertionMode = InsertionMode.Replace,
                            LoadingElementId = "loading",
                            HttpMethod = "GET"
                        })
    

    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.

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Sep 15, 2014 @ 09:55
    Ismail Mayat
    0

    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

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 15, 2014 @ 12:15
    Jeroen Breuer
    0

    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

     

     

  • Roald 6 posts 25 karma points
    Sep 15, 2014 @ 12:32
    Roald
    0

    After adding:

                    @System.Web.Optimization.Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")
    

    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?

Please Sign in or register to post replies

Write your reply to:

Draft