Store a value in ViewBag from javascript

后端 未结 2 1701
广开言路
广开言路 2021-02-19 02:05

How can I store a value in the ViewBag accessing it from javascript?

2条回答
  •  时光取名叫无心
    2021-02-19 02:44

    You cannot store a value in ViewBag from javascript. ViewBag is a server side concept and exists only on the server. Javascript runs on the client. As far as storing some data from ViewBag into a javascript variable is concerned you could use the following:

    
    

    Now this being said I always advice people against using ViewBag/ViewData in ASP.NET MVC. I recommend using strongly typed view and view models. So your code will look like this:

    @model MyViewModel
    
    

提交回复
热议问题