Returning javascript from ASP NET MVC

I have seen developers wonder how to return javascript from the server and have executed on client in an ASP NET MVC application. Fortunately, we have JavascriptResult in the ASP NET MVC stack which is responsible for doing exactly this.  It will make your life easier, but I am not advocate for  this as there are better ways doing this using Javascript on the client.

Anyway, here is how you can go about this in a few short lines of code


@Ajax.ActionLink("display", "Display", new AjaxOptions())


@Ajax.ActionLink("checks", "Sample", new AjaxOptions())


public JavaScriptResult Display()


const string script = "$('#message').append('Display');";

return JavaScript(script);


public JavaScriptResult Sample()


string script = "var textboxvalue=$('#name').val();";

script += "$('#message').append(textboxvalue);";

return JavaScript(script);



1) Clicking on the first link will display Display in the message div.

2) Clicking on the second link will display mazhar in the message div.

Happy coding and see you here again 🙂


Tags: ,

About Piusn

Enthusiastic Software Developer

What's your thought on the subject?

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: