0

I am using an mvc application and in _Layout.cshtml page I have some javascript I wanna pass it a value of

 System.Configuration.ConfigurationManager.AppSettings["anID"].ToString()

here

<script type="text/javascript">
            var _gaq = _gaq || [];
            _gaq.push(['_setAccount',PASS THAT HERE!]);

How do I do this? I am not sure how to pass data to javascript??

2 Answers 2

3

You could try like this:

<% 
    var value = System.Configuration.ConfigurationManager.AppSettings["anID"].ToString();
%>
<script type="text/javascript">
    var _gaq = _gaq || []; 
    _gaq.push(['_setAccount', <%= value %>]);
</script>

or with the Razor view engine:

@{ 
    var value = System.Configuration.ConfigurationManager.AppSettings["anID"].ToString();
}
<script type="text/javascript">
    var _gaq = _gaq || []; 
    _gaq.push(['_setAccount', @value]);
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

In situations where you have more javascript than that, and are keeping it in a separate file from the view, you can also do this:

<script type="text/javascript">
    var _anId = @System.Configuration.ConfigurationManager.AppSettings["anID"].ToString();
    var _somethingElseNeededInJsFile = @Model.SomethingNeeded;
</script>

Then in the separate javascript file, you can reference the variables easily.

var _gaq = _gaq || [];
_gaq.push(['_setAccount', _anId]);

alert('You needed this:  ' + _somethingElseNeededInJsFile);

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.