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();
This question is related to
c#
javascript
asp.net
asp.net-mvc-3
viewbag
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.
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();
try: var cc = @Html.Raw(Json.Encode(ViewBag.CC)
<script type="text/javascript">
$(document).ready(function() {
showWarning('@ViewBag.Message');
});
</script>
You can use ViewBag.PropertyName in javascript like this.
Source: Stackoverflow.com