User907863
User907863

Reputation: 417

ASP.NET MVC - Returning JavaScriptResult doesn't work

If I try to return some JavaScript from my Controller like this:

public ActionResult DoSomething()
{       
    return JavaScript("alert('Hello world!');");            
}

I do not view the alert message in my browser, but I get a download request for a .js script from the page named as the action (register.js in my case). What is wrong?

Upvotes: 7

Views: 44951

Answers (5)

Sylvia
Sylvia

Reputation: 1022

When you are calling your action from the view, make sure you use @Ajax.ActionLink instead of the @Html.ActionLink helper method. The JavaScript will then render correctly and your alert will display.

Upvotes: 1

Amir Chatrbahr
Amir Chatrbahr

Reputation: 2370

If you need to return a js code using JavaScriptResult, keep in mind that your action should be called via ajax call. Calling this action via Html.Actionlink or other controls directly, results in showing your script text instead of running the script. So you need to add a js code in your view:

<script type="text/javascript">
    $(function () {
          $('#myButtonId').click(function () { 
                $.getScript("/MyController/DoSomething"); 
           });
    });
</script>

and your action in MyController:

public ActionResult DoSomething()
{       
    return JavaScript("alert('Hello world!');");            
}

Upvotes: 3

Carl Heinrich Hancke
Carl Heinrich Hancke

Reputation: 2810

I had a similar problem with the specified JavaScript not executing when returning the result as a JavaScriptResult. In my case, the JavaScript content was rendering as text inside <pre> tags.

The solution is to return the JavaScript as a ContentResult, by using the Content() method. So try:

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

I found the answer on the ASP.NET forums. Have a look at Bruce's answer at the following link for a more complete explanation of why it gets done this way:

Return JavascriptResult Not Working

Upvotes: 14

Sparkle
Sparkle

Reputation: 2469

Try the below.

public ActionResult DoSomething(){

return Json(new {isok=true, message="Your Message", data=...});

//True / False Json Return
//return UserObj == null ?
//Json(true, JsonRequestBehavior.AllowGet) :
//Json(string.Format("YourObject '{0}' to String", YourObject),
//JsonRequestBehavior.AllowGet);

}

//view
$.ajax
{

//code
success :function(returnvalue)
{

if(!returnvalue.isok)
{
window.alert("Error:" + returnvalue.message);
}
else
{
//do the stuff with returnvalue.data
}

}

Upvotes: 3

Martin
Martin

Reputation: 11041

I would not return Javascript, I would return Content, then on the page, I would convert that content to an alert:

public ActionResult DoSomething()
{       
    return Content("Hello world!");            
}

$.ajax({
    url: "/Action/DoSomething/",
    type: "POST",
    success: editSuccess,
    error: editFailure
});

function editSuccess(data) {
   alert(data);
}

Upvotes: 7

Related Questions