Copied to clipboard

Flag this post as spam?

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


  • Akeem 43 posts 198 karma points
    Apr 14, 2016 @ 14:37
    Akeem
    0

    Jquery Ajax Call to send data to the method when the a href clicked

    Hi ,

    Please can someone help me . Am trying to send the language type clicked from my master page into the surfacecontroller in the App_code folder . My Html is like this below .

    So what am thinking is getting the class name and send it via AJAX call when the a class is clicked . i have three class="lang=en-CA" class="lang=fr-CA" class="lang=en-US"

       <div class="navbar-topbar clearfix">
            <div class="h5 pull-right"> <span class="linkCA"><strong>CA</strong> <a class="lang=en-CA" href="@NewUrlLink">EN</a> | <a class="lang=fr-CA" href="@NewUrlLink">FR</a></span> <span class="linkUS"><strong>US</strong> <a class="lang=en-US" href="@NewUrlLink">EN</a></span> </div>
        </div>
    

    i have this method , which i want to pass the type of language clicked to

      public  string GetDictionaryItemByCulture(string key, string language)
        {
    
    
            var currentLang = System.Threading.Thread.CurrentThread.CurrentCulture.ToString();
            var otherLang = myCulture(language);
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(otherLang);
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(otherLang);
    
           // String noLastSegment = Request.Url.AbsolutePath;
            string x = new UmbracoHelper().GetDictionaryValue(key);
    
    
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(currentLang);
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(currentLang);
    
            return x;
        }
    

    And i saw this Ajax method online , looks like what i need but i don't know how to wire it to work with my need .

    enter image description here

    please help me somebody ..

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Apr 15, 2016 @ 08:10
    Dennis Adolfi
    0

    Hi Akeem.

    What is it that you would like to fetch with ajax? To me your code example looks like a language toggle one usally has at the top of a website, but i might be wrong?

    As soon as i know more exact what you want to build, i will do my best to help you. :)

  • Akeem 43 posts 198 karma points
    Apr 15, 2016 @ 13:02
    Akeem
    0

    Hi Dennis,

    Yes you're right . Its language toggle . I have kept list of dictionary items . The way am doing it is that i used the AbsolutePath url as my key .

    So i first check which langauge is clicked by the user , send in the language picked (maybe fr-CA ) via ajax to my c# method with the key(which is the absolutePath url of the current site) to retrieve the fr-CA(language picked version) of the current site .

    Please help me

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Apr 15, 2016 @ 13:29
    Dennis Adolfi
    0

    Ok, im still not sure if i understod your flow completley, but here is a example of how to send data with ajax to a surfacecontroller, based on your example. (You might have to tweak it a little, if ive misunderstood someting).

    Is your GetDictionaryItemByCulture() method inside a SurfaceController? In that case you could do something like this:

    $(function() {
            // Click event for the a tags
            $(".navbar-topbar a").on("click", function(e) {
                e.preventDefault();
    
                var key = $(this).attr("class"); // Fetch the class from the clicked a tag
                var language = $(this).text(); // Fetch the value from the clicked a tag
    
                $.ajax({
                    url: '@Url.Action("GetDictionaryItemByCulture", "CustomDictionary")',
                    type: 'GET',
                    data: { 'key': key, 'language': language, 'timestamp': new Date().getMilliseconds() },
                    success: function(result) {
                        alert(result); // Do something with the result
                    }
                });
            });
        });
    

    (In this example my SurfaceController is named "CustomDictionaryController". This example requires JQuery.)

    Hope this points you in the right direction. Best of luck to you!

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Apr 15, 2016 @ 13:33
    Dennis Adolfi
    0

    Or, if you are in a script file and dont have access to the UrlHelper class, replcae the ajax url with:

    url: '/Umbraco/Surface/CustomDictionary/GetDictionaryItemByCulture',
    
  • Akeem 43 posts 198 karma points
    Apr 15, 2016 @ 13:39
    Akeem
    1

    Thank you somuch Dennis ,

    However, is it possible to send in c# data with the data sent by ajax to the GetDictionaryItemCulture .

    for example , send in this --> (DictionaryItem value)

    string DictionaryItem = Request.Url.AbsolutePath;

    i have this in my master Page since i can only retrieve this value in the view .

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Apr 15, 2016 @ 13:46
    Dennis Adolfi
    0

    Yes, there is no problem. You can pass in parameters with C#/Razor data like this:

    data: { 'key': '@DictionaryItem' },
    
  • Akeem 43 posts 198 karma points
    Apr 15, 2016 @ 14:00
    Akeem
    0

    Hi Dennis,

    Am so sorry being an amateur in using ajax .

    please if i want to retrieve the values from the ajax into the method ,how do i retrieve the value inside here . Am sorry lol

      public  string GetDictionaryItemByCulture(string key, string language)
        {
    

    is it :

    key : data.key
    language : data.dictinaryItem 
              }
    
  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Apr 15, 2016 @ 14:11
    Dennis Adolfi
    100

    No problem. Happy to help.

    No when you pass the values from the ajax to the action, you dont have to (and cant) access the data object anymore, just use the string parameters key and language directly.

    So, in your ajax data object, you define the parameters you want to send to the action. So lets say you would like to pass in one more paramters, you wite.

    data: { 'key': '@DictionaryItem', 'language': @Language, 'extraParameter': 'something' },
    

    In your ajax, and then you can access the extraParameter in your action like this:

     public  string GetDictionaryItemByCulture(string key, string language, string extraParameter)
        {
    // Do something with the parameters here.
    }
    
  • Akeem 43 posts 198 karma points
    Apr 15, 2016 @ 17:31
    Akeem
    1

    Thank Dennis, Youre the best . Its Working haha

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Apr 15, 2016 @ 18:22
    Dennis Adolfi
    0

    Thank you Akeem! Awsome! Ajax is really fun to work with! Good luck with the rest of your Umbraco site, and have a great day!!

Please Sign in or register to post replies

Write your reply to:

Draft