8

I need one Action return a JavaScript fragment.

In MVC 5 we have:

return JavaScript("alert('hello')");

but in MVC 6 we don´t.

Is there a way to do this now ?

3
  • return Content("<script language='javascript' type='text/javascript'>alert('Hello world!');</script>"); Commented Sep 18, 2015 at 19:02
  • @din Why don't you post it as an answer? Commented Sep 18, 2015 at 19:03
  • @Christos as you wish :-) Commented Sep 18, 2015 at 19:05

3 Answers 3

9

This can be achieved by returning ContentResult MSDN

return Content("<script language='javascript' type='text/javascript'>alert('Hello world!');</script>");

or other way would be making use of ajax

return json(new {message="hello"});  

 $.ajax({
        url: URL,
        type: "POST",
        success: function(data){alert(data.message)},    
    });
Sign up to request clarification or add additional context in comments.

5 Comments

Can you please include an explanation with this as well. Code only answers are typically frowned upon because they lack reasoning. Perhaps include a link to MSDN or something that references Content?
The first option does not work with mvc 6. Nothing happens, at all.
Can you add more to your code rather than a simple return statment, client side, controller code. I would prefer second option though.
Actually I need to render a partial view and present it in a bootbox control.
where do I put the ajax code. I don't have a view. I just want to display alert.
6

I think we can implment JavaScriptResult ourselves since it is not supported officially. It is easy:

public class JavaScriptResult : ContentResult
{
    public JavaScriptResult(string script)
    {
        this.Content = script;
        this.ContentType = "application/javascript";
    }
}

1 Comment

In your controller you must be add: public class JavaScriptController : Controller { public IActionResult Index() { return new JavaScriptResult("var jjj=123;console.log(jjj);"); } } in html page you must be add: <script type="text/javascript" src="/JavaScript/Index"> </script>
0

Currently ASP.NET MVC 6 doesn't support JavaScriptResult like in MVC 5. An interesting discussion for this can be found here (there's some solutions for your problem too): https://github.com/aspnet/Mvc/issues/2953

Personally I think that sending JS code to the client is a bad thing (send the client the data that the JS needs and then perform the functions invocations there) but it seems that there's a valid situation for this (look at the last comment).

2 Comments

This will be a challenge for my actual project. But I will give a try.
Sending??? No! It should be used for sharing of dynamically create JS using site endpoint. For example for dynamically created constants.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.