Description:In this example we explain that how to Access ViewBag from JavaScript or how do I access ViewBag data form JavaScript (JS).or is it possible to access MVC ViewBag object from JavaScript file.
how to access @Viewbag data to JavaScript function at client side or how to access ViewBag at client side in MVC.pass viewbag data to external JavaScript file or access viewbag in JavaScript code there are lots of questions are raised in our mind.
So below is the code that demonstrates how to use ViewBag in JS.
Code:
<script>var myJsVariable
= '@ViewBag.MyVariable' </script>
2) var cc = @Html.Raw(Json.Encode(ViewBag.CC)
3) <script type="text/javascript">
$(document).ready(function() {
showWarning('@ViewBag.Message');
});
</script>
4) var myValue = document.getElementById("@(ViewBag.CC)").value;
5) var x = $('#' + '@(ViewBag.CC)').val();