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?
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.
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.
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#. 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() {.
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.
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.
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.
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";
}
}
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