38

My attempted methods.

Looking at the JS via browser, the @ViewBag.CC is just blank... (missing)

        var c = "#" + "@ViewBag.CC";
        var d = $("#" + "@ViewBag.CC").value;
        var e = $("#" + "@ViewBag.CC").val();

        var c = "@ViewBag.CC";
        var d = $("@ViewBag.CC").value;
        var e = $("@ViewBag.CC").val();

7 Answers 7

75

if you are using razor engine template then do the following

in your view write :

<script> var myJsVariable = '@ViewBag.MyVariable' </script>

UPDATE: A more appropriate approach is to define a set of configuration on the master layout for example, base url, facebook API Key, Amazon S3 base URL, etc ...```

<head>
 <script>
   var AppConfig = @Html.Raw(Json.Encode(new {
    baseUrl: Url.Content("~"),
    fbApi: "get it from db",
    awsUrl: "get it from db"
   }));
 </script>
</head>

And you can use it in your JavaScript code as follow:

<script>
  myProduct.fullUrl = AppConfig.awsUrl + myProduct.path;
  alert(myProduct.fullUrl);
</script>
4
  • 4
    Why the @ before the <script> ?
    – gdoron
    Apr 4, 2012 at 9:07
  • Minus the @ symbol before Script, and this is the correct answer. Thanks
    – IAmGroot
    Apr 4, 2012 at 9:17
  • 1
    you need the @ sign when using traditional <script> tags, just tested May 28, 2015 at 19:11
  • @amd this is very helpful. +1
    – Hadi
    Jun 30, 2017 at 15:41
14

try: var cc = @Html.Raw(Json.Encode(ViewBag.CC)

2
  • @Doomsknight. And I hope you won't use it.
    – gdoron
    Apr 4, 2012 at 9:14
  • 1
    Well in that case, check the value of ViewBag.CC server side, you might not be setting it in some case.
    – ZeNo
    Apr 4, 2012 at 9:15
9
<script type="text/javascript">
      $(document).ready(function() {
                showWarning('@ViewBag.Message');
      });

</script>

You can use ViewBag.PropertyName in javascript like this.

2
  • Im not sure wht showWarning does. (didnt do anything for me) but the rest did contain the correct value. So I saw showWarning('2'); in my code.
    – IAmGroot
    Apr 4, 2012 at 9:17
  • 1
    I was using this code, it calls just alert and it works fine. you can change alert('@ViewBag.Message'); it will work.
    – adt
    Apr 4, 2012 at 9:20
4

ViewBag is server side code.
Javascript is client side code.

You can't really connect them.

You can do something like this:

var x = $('#' + '@(ViewBag.CC)').val();

But it will get parsed on the server, so you didn't really connect them.

5
  • I think you have an additional bracket? Ive tried similar to this but with ".
    – IAmGroot
    Apr 4, 2012 at 9:08
  • @Doomsknight. ' or " is the same thing. Well Did it work?
    – gdoron
    Apr 4, 2012 at 9:09
  • On run time i see var c = $('#' + '2').val();
    – IAmGroot
    Apr 4, 2012 at 9:11
  • @Doomsknight. O.k. So it's working... :) You have 2 in the ViewBag.CC
    – gdoron
    Apr 4, 2012 at 9:13
  • Seeing the 2 now, Ive gone back to var c = '@ViewBag.CC'; seems to be working now :/ Maybe running a few methods together were breaking it all. Thanks
    – IAmGroot
    Apr 4, 2012 at 9:14
3

You can achieve the solution, by doing this:

JavaScript:

var myValue = document.getElementById("@(ViewBag.CC)").value;

or if you want to use jQuery, then:

jQuery

var myValue = $('#' + '@(ViewBag.CC)').val();
3
  • 1
    I guess the javascript alternative might help others +1. Also i think you can just write '#@(ViewBag.CC)'
    – IAmGroot
    Jan 10, 2017 at 12:31
  • Yeah, you can be right @Doomsknight, because world consisting multiple solutions :-) Jan 11, 2017 at 7:08
  • yes @Doomsknight, You're right. I was just try to elaborate is an easy way. ;-) Jul 1, 2018 at 18:21
0

None of the existing solutions worked for me. Here's another solution I found that did work:

Controller:

TempData["SuccessMessage"] = "your message here";

View:

let msg = '@TempData["SuccessMessage"]';
0

Try this:

Anywhere in HTML: <input hidden [email protected] id="CC_id" />

In JS: var CC= document.getElementById("CC_id").value.toString();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.