Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

3 Answers

up vote 10 down vote accepted

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.

share|improve this answer
2  
This doesn't actually answer the request to provide a working example. Doing this makes perfect sense for a Javascript MVVM application that just uses MVC to initially load html/scripts and as an API. – Kasey Speakman Jul 12 '12 at 17:59
1  
If I understand correctly, this is the same goal of the RequireJS library. – Carl G Oct 2 '12 at 19:16

Here's a practical case: I have a Global static class that contains static properties of values that are used through the whole system.

Some of those values need to be shared with JS code. So I created an Action that returns JavaScriptResult.

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

And in the page footer I load the page:

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

Now I can get the values in any Javascript file:

if(ItemsPerPage == 25)
{
   alert('it works!');
}
share|improve this answer

Check out my reply in this post;

MVC how to return instruction to run javascipt method?

That will return a partial view to the page. If you want to itterate through a json object then return a json object from your controller and use something like the following;

var obj = eval('(' + msg + ')');

msg above is the returned object from your controller;

then,

$.each(obj.Objects, function() { do something with object });

"Objects" above is a property within the returned json object.

So in c#

public class JsonObject()
{
  List<MyObjectList> Objects{get;set;}
}

Return the above object to the view.

Does this make sense or would you like a working sample?

share|improve this answer
i remember when i was researchign this, there was only one example in code, abotu a month ago anyway, and it was a video example by david hayden, the video wasn't even specifically for this demonstration. am really suprised by the lack of code examples for this feature. – Erx_VB.NExT.Coder Feb 24 '10 at 22:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.