Quick Introduction of ViewBag
ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0. ViewData requires typecasting for complex datatypes and checks for null values to avoid errors.
Basically, ViewBag and ViewData are similar in use when we use them to pass data from Controller to View.
Controller Class
Accessing ViewBag in Cshtml file
Output
Accessing ViewBag Value in JavaScript
Added External JavaScript file
Access Viewbag Value in External JavaScript file,
It will throw an error ExternalJs.js:1 Uncaught SyntaxError: Invalid or unexpected token.
So, create a JavaScript global variable and store the ViewBag and ViewData value in that global variable. Then, access that JavaScript variable in external JavaScript.
Final Output
Manali GhugarePosted Dec 19, 2019, 3:58 AM
How can i access viewbag list value in external js
h zPosted Oct 10, 2019, 1:31 PM
How to pass an array of numbers or strings from controller to View via viewbag, and access these values in the javascript in the view?
Nazar TvmPosted Jul 26, 2016, 8:43 AM
Thanks :)
kalu singh raoPosted Jul 25, 2016, 8:09 AM
Good starting