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"
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 .
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. :)
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 .
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!
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.
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"
i have this method , which i want to pass the type of language clicked to
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 .
please help me somebody ..
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. :)
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
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:(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!
Or, if you are in a script file and dont have access to the UrlHelper class, replcae the ajax url with:
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 .
Yes, there is no problem. You can pass in parameters with C#/Razor data like this:
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
is it :
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.
In your ajax, and then you can access the extraParameter in your action like this:
Thank Dennis, Youre the best . Its Working haha
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!!
is working on a reply...