Difference between viewbag and viewstate?

后端 未结 3 1095
遥遥无期
遥遥无期 2020-12-30 04:38

Is there a functional difference between ViewState in Webforms and ViewBag in MVC? They seem to be the \"same thing\". And can be used in the same ways. I ask because MVC pr

3条回答
  •  轮回少年
    2020-12-30 04:48

    ViewState in Web Forms was serializing form data into a hidden, encrypted field in the form, so data could be re-bound on the postback.

    ViewBag/ViewData is a dictionary where you can "stuff" data into. For example, you might add to it in your Controller, then access it in your View. The data is dynamic which makes it difficult to work with the data. ViewBag doesn't get sent to the client, it's part of the MVC (server pipeline).

    Both should be avoided.

    ViewState by, well, not using it and finding workarounds. And ViewBag should be avoided by the use of ViewModels.

提交回复
热议问题