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.
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.
Another potentially dangerous ActionResult is JavaScriptResult, which returns a script that is to be executed by the browser.
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" />.
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.
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!'); }
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 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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With