Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
30
rated 0 times [  32] [ 2]  / answers: 1 / hits: 18017  / 11 Years ago, tue, march 26, 2013, 12:00:00

I have one Viewbag. I have fill that viewbag value from server side in Action result method. I need to access this Viewbag value in Js file. I have access this Viewbag in *.cshl page properly. Here below shown my sample code,




Var objMode = '@ViewBag.Mode';
//Written in *.cshtml page.



but i need to access this value like above syntax in *.js file.



Thanks,
Nirav Parikh


More From » asp.net-mvc

 Answers
47

You can't. You can write ViewBag value in hidden input and then read it from js file:



<input type=hidden [email protected] id=mode />


JS file:



var mode = document.getElementById('mode').value;


EDIT: Another option:



<script src=... type=text/javascript onload=InitMyScript('@ViewBag.Mode')></script>


JS file:



function InitMyScript(mode){
//other code here
}

[#79354] Monday, March 25, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anitadevonm

Total Points: 730
Total Questions: 93
Total Answers: 74

Location: Estonia
Member since Wed, Jun 8, 2022
2 Years ago
;