javascript - How to check ViewBag's property in a cshtml page? -


i working on mvc application , need check viewbag value on cshtml page, how can that?

on controller.cs:

viewbag.mode= "edit"; 

i need check value viewbag.mode if edit show alert edit

i writing conditional code in javascript, syntax check viewbag property in cshtml?

you don't direct access c# variables in javascript 1 server-side, other client-side. need write viewbag value html output , interrogate dom find using javascript. this:

in cshtml:

@html.hidden("mode", (string)viewbag.mode, new { id = "mode" }) 

in jquery:

if ($('#mode').val() == "edit") {     // something... } 

you wrap js code directly in c# if (viewbag.mode == "edit") statement in view, both ugly, , not separation of concerns.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -