Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working example for JavaScriptResult in asp.net mvc

Tags:

Can somebody provide a working example of JavaScriptResult in asp.net mvc. I understand that it returns javascript which is then executed on the client side and also that the content type of the response is set to text/javascript. I need some working example to see this thing in action.

like image 583
Ravinder Singh Avatar asked Nov 04 '09 23:11

Ravinder Singh


People also ask

What is JavaScriptResult MVC?

JavaScript result sends JavaScript content to the response. Here we create one div element in the index. cshtml page. We write some text inside the div element. In the JavaScript result method we get a div element and update its content using JavaScript.

What is JavaScriptResult?

Another potentially dangerous ActionResult is JavaScriptResult, which returns a script that is to be executed by the browser.

How can call JavaScript function on button click in ASP.NET MVC?

In MVC we rarely uses onclick event on input element, usually we use jQuery like this: $("#elementname"). click(function () { alertMe(); }); <input value="親コード新規登録" type="button" id="elementname" />.

What is MVC in asp net with example?

The MVC convention is to put controllers in the Controllers folder that Visual Studio created when the project was set up. Let's take a look at a simple example of Controller by creating a new ASP.Net MVC project. Step 1 − Open the Visual Studio and click on File → New → Project menu option.


2 Answers

Note: This answer was written in 2011 and looking at it nowadays, it's more of a hack. It's better to load values through AJAX request that hits a JSON endpoint API.

Here's a practical case: I have a GlobalSettings static C# class that contains static Properties of values that are used through the whole system in the ASP.NET MVC backend side of things.

Some of those values need to be shared with JS code. So I created an Action that returns JavaScriptResult which basically pumps out those values into global JS variables.

Note: Change the output cache period to suit your needs

[OutputCache(Duration = 999999)] public virtual JavaScriptResult Global() {         var script = $@"             MaxNotificaitonsToShow = {GlobalSettings.MaxNotificaitonsToShow};             ItemsPerPage = {GlobalSettings.ItemsPerPage};         ";     return JavaScript(script); } 

And then I load the response of this action as a JS file inside all pages through the HTML footer:

<script type="text/javascript" src="/JS/Global"></script> 

Now I can get the values in any Javascript file:

if(ItemsPerPage == 25) {    alert('it works!'); } 
like image 156
Korayem Avatar answered Oct 08 '22 21:10

Korayem


Avoid if possible

JavaScriptResult is considered an anti-pattern that Asp.net MVC introduced (complete separation of concerns), because it couples Controller and View back together to make them dependable on eachother. In a pure Asp.net MVC application where the UI is build on Asp.net MVC and server side serves this client implementation only it is thus advised to avoid this functionality.

It may be useful in other scenarios. I can remember I've been reading something related to Ruby on Rails clients.

Anyway.

An example that does make sense

An actual example would be to return javascript code to an Ajax request that would simply provide some functionality that will get executed immediately upon response without any data manipulation.

Where could you possibly benefit from it? Well think of an application that has huge amounts of various client classes used thoughout the application. But certain pages use only a small fraction (or even a dynamic fracion) of them. In this case you would have two possibilities:

  1. Load the whole client class tree upfront - either in a huge single file or fragmented in separate files (this would be ok if views would use a small sub set of up-front known classes, because otherwise this would result in lots of server requests)
  2. Load classes on demand when they are needed - or maybe even execute certain class functions on demand when and if they are needed.

In this particular case, the second scenario would be much better and much more efficient in terms of network traffic, client memory resources and processor load.

like image 22
Robert Koritnik Avatar answered Oct 08 '22 22:10

Robert Koritnik