Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return JavaScript from controller?

I'm working with the ASP.NET Core 2.2 project where I need to return JavaScript from the controller. However, I suspect that there is no direct way, hence, I followed this OS answer and changed my code as following:

public IActionResult MyAction()
{
    var sb = new StringBuilder();
    sb.Append("$(document).ready(function(){");
    sb.Append("alert('hi')");
    sb.Append("});");            
    return new JavaScriptResult(sb.ToString());
}

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

Though it's just writing plain text instead. Is there way around?

like image 384
Divyang Desai Avatar asked Aug 14 '19 07:08

Divyang Desai


People also ask

What is JavaScript result in 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.

How do I return a controller model?

To return a model from the controller action method to the view, we need to pass the model/object in the View() method. Here, the View gets the model as UserNamePasswordModel object with its property set. To retrieve properties of this object in the view, we can use @Model ; like @Model.

CAN controller return string?

You can't return a string from a method which returns an ActionResult, so in this case you return Content("") as swilliams explained.

How to call JavaScript function from the controller using C #?

How to Call JavaScript function from the Controller using C#. If the JS function you need to call doesn’t return any value, you can just render the function using JavaScript ActionResult: public ActionResult MyAction () { return JavaScript ("window.alert ('Hello World');"); }. public ActionResult MyAction() {.

How do I return a value from a JavaScript function?

Once the view is rendered in the browser, it doesn’t have any communication with the controller. If the JS function you need to call doesn’t return any value, you can just render the function using JavaScript ActionResult: In case you have to return a value from JS function, the only solution here is to use a ViewBag.

How to get the return value from the method in controller?

So my question now is, how do I get the get the return value from the method in the controller? Change your controller method to have return type ActionResult or JsonResult and I prefer JsonResult would be enough here and retrun Json response from controller and manipulate this method with $.get.

How to use outputjavascriptalert in the controller?

When the link “Test JavaScript” is clicked, it calls the OutputJavaScriptAlert action method of the controller. This action method returns JavaScript content that we get it in the new window or to download as displayed in the above picture (depending on the browser). In general, these methods are used in the Ajax methods to return partial content.


1 Answers

You can use ajax to load the javascript , in your page :

<script>
    $(function () {
            $.getScript("/Controller/Action");
    });

</script>

Your serve side :

public IActionResult DoSomething()
{
    return new  JavaScriptResult("alert('Hello world!');");
}

public class JavaScriptResult : ContentResult
{
    public JavaScriptResult(string script)
    {
        this.Content = script;
        this.ContentType = "application/javascript";
    }
}
like image 199
Nan Yu Avatar answered Oct 03 '22 19:10

Nan Yu