Copied to clipboard

Flag this post as spam?

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


  • Matt Taylor 873 posts 2086 karma points
    Nov 05, 2013 @ 11:09
    Matt Taylor
    0

    How to call a Template or Razor script with Ajax?

    I've used Ajax before to call webmethods or base functions but I'd like to try using Ajax to call a Razor script which will generate some HTML and then display that HTML on my page.

    I'm looking for an exmple to show me how to do it.

    Regards,

    Matt

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Nov 05, 2013 @ 12:49
    Ali Sheikh Taheri
    100

    Hi Matt,

    If you are using Umbraco 6 then you can create an action in SurfaceController which returns the partial view. more here

    and if you are using Umbraco 4 the way I usually do it is that I create a template and with ajax request to that template. you dont need to create a page for it, just the template would be enough.

    hope it gives you some idea.

    Cheers

    Ali

  • Matt Taylor 873 posts 2086 karma points
    Nov 05, 2013 @ 13:45
    Matt Taylor
    1

    Hi Ali,

    I'm using 6 with webforms.

    I've given it a quick test with the template and it looks pretty easy.

    With this script in my html (don't forget to add a reference to Jquery):

    <script type="text/javascript">
        $(function () {
            $('#Button').click(testScript);
        });

        function testScript() {
            $.post("/mypage.aspx?altTemplate=AjaxTest",
            function (data) {
                alert(data);
            });
        }
    </script>

    and this in my AjaxTest template:

    <%@ Master Language="C#" MasterPageFile="~/umbraco/masterpages/default.master" AutoEventWireup="true" %>

    <asp:Content ContentPlaceHolderID="ContentPlaceHolderDefault" runat="server">
        <p>Hello</p>
    </asp:Content>

    the alert displayed the HTML from the template niceley.

    Regards, Matt

     

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Nov 05, 2013 @ 13:51
    Ali Sheikh Taheri
    0

    Hi Matt,

    Glad it worked.

    you should be able to use the altTemplate like this:

    "/mypage.aspx/AjaxTest"
    

    this used to work on v4 but not sure about v6. could you try this too and let me know if that's still valid?

    Thanks

    Ali

  • Matt Taylor 873 posts 2086 karma points
    Nov 05, 2013 @ 14:47
    Matt Taylor
    0

    No, that does not work.

    Maybe it's an MVC feature not webforms.

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Nov 05, 2013 @ 15:22
    Ali Sheikh Taheri
    0

    Thanks for trying though :)

Please Sign in or register to post replies

Write your reply to:

Draft