0

I have a js file called: myfile.js
It's content looks like:

function fnGetFilterVals(){
    var f = '?';
    f += '&fltInsertDate=' + $('#fltCreateDate').val();
    f += '&fltStatusID=' + $('#fltStatus').val();
    @if (ViewBag.GetCompanyID == -1)
    {
        <text>f += '&fltCompanyIDs=' + $('#fltCompany').val();</text>
    }
    return f;
}

I create a bundle in BundleConfig.cs:

bundles.Add(new ScriptBundle("~/bundles/myfile").Include("~/static/asset/myfile.js"));

After that, I include it to view "@section Scripts":

@Scripts.Render("~/bundles/myfile")

Since, my file contains @ character, page raises "Uncaught SyntaxError: Unexpected token ILLEGAL" error.

@if (ViewBag.GetCompanyID == -1)  

How can I use @ character in a separated js file?

1
  • Make a code block: @{ if (..) {...} }. Commented Mar 13, 2014 at 12:03

1 Answer 1

2

It is possible to do it with RazorJS but it isn't probably what you are looking for. The bundle means that it will compress all you javascript files in a bundle and don't touch them ever again until they have changed on the files system. Which means that your ViewBag wouldn't be updated.

You should have only libraries in separate javascript files and add the specific calls of function in your view, with the correct parameters. You should instead send the ViewBag.GetCompanyID from the View as a parameter on your fnGetFilterVals function.

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

3 Comments

RazorJS includes js file to the page. I also want to compress the code. I think RazorJS does not compress.
I don't know about the details, but don't think so. Though you would need to render the js file for every new request so that the ViewBag.CompanyID property with the if condition is up to date. This is in no way efficient compared to a simple javascript parameter.
ok. thanks. i was hopeful about i can find a way to do this, but as i see there is no.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.