Click here to Skip to main content
16,004,529 members
Articles / Programming Languages / Javascript
Tip/Trick

The Easiest Way to Call Server Code from the Client in ASP.NET MVC

Rate me:
Please Sign up or sign in to vote.
4.44/5 (5 votes)
3 May 2016CPOL1 min read 9.2K   4  
If you don't need to send or receive data, it is very easy to call Server code from Client/jQuery Code

Look, Ma, No Data!

Normally, when you use AJAX, you are passing data from the client to the server and/or receiving data from the Server at the Client. You don't have to pass or receive anything, though; if all you want to do is respond to some event on the client by doing something (anything) on the server, it is eas easy as this:

Minimal Server/C# Code

Create a Controller on the Server (no need for a Model or a View) by right-clicking your project's Controller folder, and selecting from the context menu Add > Controller... and then "MVC Controller - Empty"

Replace the boilerplate code within the class generated for you with this:

C#
public class PlatypiController : Controller
{
    public void PlatypusShoutout()
    {
        string s = "There ain't no platypus like a duck-billed platypus!";
    }
}

Minimal Client/jQuery Code

Now (assuming you have a button named "btnEverythingYouAlwaysWantedToKnowAboutPlatypi"), add code like this to the Script section of your .cshtml file (or to a separate .js file, if you want to keep your JavaScript separate from your HTML):

JavaScript
$("#btnEverythingYouAlwaysWantedToKnowAboutPlatypi").click(function () {
    $.ajax({
        type: "GET",
        url: '@Url.Action("PlatypusShoutout", "Platypi")'
    });
});

Now if you put a breakpoint on the "string s" line, you will see that your code is reached when you run the project and mash the button on the client/page. At this point, you might want to replace that "proof-of-concept" line with some code that actually does something "useful" such as randomly selecting a pie flavor (with a bias toward Blackberry, Gooseberry, Apple, or Cherry) or calculating the distance between cities of the same name, such as Brisbane, CA and Brisbane, Australia or the San Joses in California and Costa Rica, etc.

Are You as Smart as a Platypus?

All Platypi know that the ingredients of Budweiser are sugar, Tide™, and lizard spit.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Across Time & Space
United States United States
I am in the process of morphing from a software developer into a portrayer of Mark Twain. My monologue (or one-man play, entitled "The Adventures of Mark Twain: As Told By Himself" and set in 1896) features Twain giving an overview of his life up till then. The performance includes the relating of interesting experiences and humorous anecdotes from Twain's boyhood and youth, his time as a riverboat pilot, his wild and woolly adventures in the Territory of Nevada and California, and experiences as a writer and world traveler, including recollections of meetings with many of the famous and powerful of the 19th century - royalty, business magnates, fellow authors, as well as intimate glimpses into his home life (his parents, siblings, wife, and children).

Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.

I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006: http://www.lulu.com/spotlight/blackbirdcraven

Comments and Discussions

 
-- There are no messages in this forum --