Copied to clipboard

Flag this post as spam?

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


  • Vincent 75 posts 98 karma points
    Jun 16, 2010 @ 04:24
    Vincent
    0

    Do ajax call

    Hi, I'm new to Umbraco and I want to know how I can handle AJAX call?

     

    As an exemple, in MVC I do it this way 

    In the view : 

    function showCompanies() {
        $.post(liveString + '/Enterprises/GetAll', function(data) {
            $('#COMPANIESLIST_CONTENT').empty().append(data);
            $('#companies_list_content').cycle('destroy');
            $('#companies_list_content').cycle({
                fx: 'fade',
                timeout: 0,
                pager: '#companies-nav'
            });
        });
    }

    In my controller : 

       [AcceptVerbs(HttpVerbs.Post)]
            public virtual ActionResult GetAll()
            {
                var nbMaxItemCol = 9;
                var nbColMax = 3;
                var nbCol = 1;
                var enterpriseUrl = ConfigurationManager.AppSettings["enterprisesUrl"];
                var i = 0;
                var j = 0;
                var enterprises = _enterprisesRepo.GetAll(true);
                var nbItemCol = enterprises.Count/3 > nbMaxItemCol ? nbMaxItemCol : enterprises.Count/3;
                var html = "<div id=\"companies_list_content\">\n";
                foreach (var e in enterprises)
                {
                    //build my enterprise HTML
                }
                if (enterprises.Count > 0)
                {
                    html += "</ul>\n</td>\n";
                    html += "</tr>\n";
                    html += "</tbody>\n";
                    html += "</table>\n";
                    html += "</div>\n";
                }
                html += "</div>\n";
                html += "<div id=\"companies-nav\"></div>";
                html += "<div class=\"companies_list_see_all\"><a href=\"" + enterpriseUrl + "\">Voir la liste complète</a></div>\n";
    
                return Content(html, "text/html");
            }

    Is it possible to do something like this with Umbraco? Like calling a custom UserControl that connect to a DB ou to Umbraco's DB and returns the data in JSON or other javascript friendly format? How do I setup a url to do so and how do I know what is the url I can call for the data?

     

    Thanks a lot!

  • Aaron Powell 1708 posts 3044 karma points c-trib
    Jun 16, 2010 @ 06:09
    Aaron Powell
    2

    Umbraco can load a .NET user control as either a macro or have it embedded in the template directly.

    There's lot of good resources on the web which will give you an introductory guide to doing AJAX with WebForms, but the long story short is you create a web service which returns what ever you require.
    And since this is a web service you can do what ever you want with it, such as query a database.

    But returning JSON is generally the best way, you're UI (the browser) should do the UI markup (HTML), not the server.

  • Vincent 75 posts 98 karma points
    Jun 16, 2010 @ 15:35
    Vincent
    0

    Thanks for the reply,

     

    I know I should not do HTML on the server side, it's not a good exemple... My question is more how do I include my webservice into Umbraco?

  • Aaron Powell 1708 posts 3044 karma points c-trib
    Jun 17, 2010 @ 00:40
    Aaron Powell
    0

    The same why in which a web service is added to any .NET site, you create an ASMX, compile it and include it in the website.

    Umbraco doesn't require anything special configured to run webservices.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies