Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Accessing View Bag And View Data Value In External Java Script File
WhatsApp
Nazar Tvm
4y
148.3
k
0
5
25
Blog
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
Java Script File
View Bag
View Data Value In External
Up Next
Create an Alert Message from Controller View Using JavaScript Alert MessageBox
Ebook Download
View all
Voice of a Developer: JavaScript From Scratch
Read by 10.9k people
Download Now!
Learn
View all
Membership not found