0

I'm using Visual Studio 2012 RC with ASP.NET MVC 4. This is driving me crazy, and I know this has been asked several times, but I can't find a solution for MVC 4, and no unified solution for MVC 3. I must be missing something...

All I want to do is output a c# variable in a block of javascript. This should be trivial, and is a necessity to be able to do, but despite all of the workaround/hacks I've tried, nothing will get rid of the "Conditional compilation is turned off " error. This would be fine if it was just a warning, but it's an error. If I close the view inside of visual studio it compiles and works fine, but if it's open I can't even compile, and that isn't acceptable.

I've tried using /*@cc_on @*/ and /*@("@cc_on @")*/ to turn conditional compilation on to no avail. Doing @(serverVar) gives an invalid character error (@). I've also tried @Html.Raw and several other things.

The only way I include razor code without an error is if the razor code is inside quotes, which works well for strings, but not for int and bool.

@{
    int serverVar = 5;
}

<script>
    var obj = { jsVar: @serverVar };
</script>

I've searched everywhere for this, and can't find an agreed upon solution. Am I missing something? Any help is greatly appreciated.

2
  • What is the error you are getting? When I paste your code block in a page (also MVC 4 on VS 2012, albeit not RC), it works for me giving var obj = { jsVar: 5 }; as the script body. Commented Jan 5, 2013 at 7:55
  • It's gotta be an RC thing then. I get a "Conditional compilation is turned off" error. When the view is open in visual studio that error displays and I can't compile. If I close the view, I can compile, and it does work as expected. However, there shouldn't be an error that prevents me from compiling in any circumstance. Commented Jan 5, 2013 at 9:35

3 Answers 3

1

Use JavaScriptModel ( http://jsm.codeplex.com )

This way you can transfer easily server-side variables to javascript without annoying inline javascript.

You would just have to write the following code in your controller action:

this.AddJavaScriptVariable("VariableNameInJavaScript", serverVar);

If you want several variables to be set on every page use a filter like it's documented here:

http://jsm.codeplex.com/wikipage?title=Use%20JavaScriptModel%20in%20a%20global%20filter&referringTitle=Documentation

Edit: If you have a lot of variables, you should categorize them and Add them in different literals. E.g.

this.AddJavaScriptVariables("Config.UserId", userId,
    "Config.SomeOtherConfig", someOtherConfigValue,
    "MyGoogleMapController.Pushpins", GetMapPushpins());
Sign up to request clarification or add additional context in comments.

7 Comments

The only way this can work is if it pollutes the global namespace (eg. adds variables to the window object). I want everything encapsulated inside one global object which I create. Nonetheless this is a very interesting solution, and i upvoted it.
Also, in their examples they still have this: <script type="text/javascript"> @this.RenderJavaScriptModelInit()</script> which means you still have to embed c# calls inside a javascript block, so the error will still show up.
I simply need to make one MyGlobalJSObj.init() call with the server vars in it. I'm thinking an html helper is probably the best way to do this.
Your code in the question pollutes the global namespace... Every code does in fact... You can only encapsulate variables. You could also use Literal objects. E.G. if you have a Config, use "Config.VariableName" as javascript variable and you get a Config object which you can use from your javascript functions.
My example was over simplified. I already have a global object created, and as long as I can add "MyGlobalObj.Var" and it doesn't overwrite my object, then that's fine. However, if you look at my second comment, using c# within a javascript block is still an issue.
|
0

Umm I guess there is no way to get rid of this error, and since it still compiles everything is just fantastic.

Comments

0

I solved it by using below trick.

var supportedNumber = Number(@TempData["supportedNumber"]);

Comments

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.