How do I access ViewBag from JS

前端 未结 5 1827
走了就别回头了
走了就别回头了 2020-12-08 19:51

My attempted methods.

Looking at the JS via browser, the @ViewBag.CC is just blank... (missing)

        var c = \"#\" + \"@ViewBag.CC\";         


        
5条回答
  •  情书的邮戳
    2020-12-08 20:06

    ViewBag is server side code.
    Javascript is client side code.

    You can't really connect them.

    You can do something like this:

    var x = $('#' + '@(ViewBag.CC)').val();
    

    But it will get parsed on the server, so you didn't really connect them.

提交回复
热议问题