Is ViewState relevant in ASP.NET MVC? Is ViewState relevant in ASP.NET MVC? asp.net asp.net

Is ViewState relevant in ASP.NET MVC?


Yes, that is correct. ViewState is not relevant. More on differencies between Page Model and MVC here:

Compatibility of ASP.NET Web Forms and ASP.NET MVC


Its present because ViewPage inherits from Page. However Page itself had no use for ViewState its used by WebControls. It is possible to include original WebControls in a View but doing so would be completely missing the point of separating control from view.


ViewState is not relevant, however it provided some great functionality. We didn't have to reload data every time, or worry about caching each item, etc.ViewState also provided some security - it prevented a certain degree of form tampering. If you bound a combo box, it stopped people from fiddling with the values as those were compared against the hashed viewstate and would fail validation if it was messed with. To this end ViewState was quite nice. The problem is it got very large on most pages as people didn't turn off viewstate for what they didn't need it for.

Ok - how to solve this?The MVC Futures project from Microsoft contains the Html.Serialize method and in conjunction with the [Deserialize] attribute as a method parameter this provided very fine grained control over 'viewstate' - ie serialization.

ex. in the controller:

 [HttpGet]        public ActionResult Index()        {            OrderRepository repository = new OrderRepository();            var shipTypes = repository.GetAllShipTypes();            var orders = repository.GetAllOrders();            ViewBag.ShipTypes = shipTypes;            return View(orders.First());        }        [HttpPost]        public ActionResult Index(Order order, [Deserialize] List<ShipType> shipTypes)        {            //Note order.ShipTypeId is populated.            ViewBag.ShipTypes = shipTypes;            return View();        }

and in the View I serialize it and ALSO use it in a combo

@Html.Serialize("ShipTypes", ViewData["ShipTypes"])        @Html.DropDownList("ShipTypeId", ((List)ViewData["ShipTypes"]).ToSelectList("ShipTypeId", "Description"), new { @class = "combobox11" })