2

I have a method that adds javascript to the end of partial views if it's an ajax request. My problem though is that when the script is returned, the script tags have been removed and the function I want to execute is written in plain text.

if (httpContext.Request.IsAjaxRequest())
        {
            script = "MyNamespace.globals.initFunctions.push(function() { " + script + " });";
            return new HtmlString(@"<script type=""text/javascript"">" + script + "</script>");
        }

So, instead of getting the desired result of

<script type="text/javascript">MyNamespace.globals.initFunctions.push(function() { MyNamespace.init(); });</script>

I get MyNamespace.globals.initFunctions.push(function() { MyNamespace.init(); }); in plain text.

What could be the reason?

Edit:

Trying it with Html.Raw() did not help. While the script string does contain the script tags, they are still removed when rendered.

    @{
    string script = ViewUtilities.AddScript("MyNamespace.init();", this.ViewContext.HttpContext);
}

    @Html.Raw(script);

Edit 2:

Now I've tried writing it all in the view like this,

<script type="text/javascript">
    MyNamespace.globals.initFunctions.push(function() { MyNamespace.init(); });
</script>

and it still removes the tags around the script and renders it as plain text. I don't know where to go from here...

When I exam the response with Firebug it looks fine but looking at the html it's not.

1
  • Did you ever figure this out? I'm getting the same thing in a plain view where my script tags are being stripped out Commented Oct 20, 2015 at 8:58

3 Answers 3

2

The HTML is being encoded which happens auto-magically with ASP.NET MVC.

Using @Html.Raw("htmlstring") should give you the raw HTML value of the string without stripping the tags.

Sign up to request clarification or add additional context in comments.

2 Comments

I've updated my question with my try of this. Sadly it did not work.
You have to have something else going on. Html.Raw is the answer. Paste this into your page. Does it work? Works here. @{ var someScript = "<script type=\"text/javascript\">alert('stuff!');</script>"; } @Html.Raw(someScript)
1

You want to use @Html.Raw(whatever function you call)

I think you also probably want to return return MvcHtmlString.Create(fullscript);

1 Comment

Did not make a difference. The returned string somehow gets its script tags removed.
0

If you convert this to an HtmlHelper method, it will work. Change your ViewUtilities class as follows:

public static class ViewUtilities
{
    public static MvcHtmlString AddScript(this HtmlHelper htmlHelper, string script)
    {
        if (htmlHelper.ViewContext.HttpContext.Request.IsAjaxRequest())
            return new MvcHtmlString(@"<script type=""text/javascript"">" + script + "</script>");
        return new MvcHtmlString("");
    }
}

Then, in the web.config file in the Views folder (and in any Views folders in any Areas), add the following to the namespaces list in the <pages pageBaseType="System.Web.Mvc.WebViewPage"> element:

<add namespace="{namespace where ViewUtilities class is located}"/>

Finally, in your view:

@Html.AddScript("MyNamespace.init();")

1 Comment

Thank you for the HtmlHelper idea. I've tried it and still no change. I'm beginning to feel that there's something else and not the code that's making this happen. No idea what it could be though.

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.