简体   繁体   中英

Call C# method from another class using JavaScript/jQuery

I have a jQuery-method that returns the text of a div when clicked on. This method is in a .js file. I want to use a method from another class that uses the text from the div that was clicked on as parameter.

JavaScript/jQuery:

$(document).on('click', '#clicked div', function () {
    var temp = $(this).html();
    temp = temp.replace('<span>', '').replace('</span>', '');

    // someMethod(temp);
});

Class.cs :

public string someMethod(string str){
    return str;
}

How do I access this method from the JavaScript file?

Make that class static and add that method ie someMethod there, and then make an ajax call to that method

public static class Yourclass
{
  public static string someMethod(string str)
  {
    return str;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM