How can I store a value in the ViewBag accessing it from javascript?
You can't. ViewBag is a server-side thing, Javascript runs on client side.