28

I am learning ASP.NET MVC and many concepts of "ASP.NET" like authentication, authorization, session state still apply to it.

But from whatever little I have understood, I don't see that ViewState is still relevant in an ASP.NET application. But it is there (property of System.Web.Mvc.ViewPage)! Is it there only for compatibility reasons or it still have some purpose/use?

Hemant
  • 19,486
  • 24
  • 91
  • 127
  • 6
    This kind of question I find very useful. I am not using ASP.NET MVC right now because I'm not presently doing web stuff at all, but I still want to know about it so I can pick it up quicker when I am using it. – NeedHack Jul 23 '09 at 10:05
  • Related to http://stackoverflow.com/a/2230635/52277 – Michael Freidgeim May 10 '12 at 21:42

5 Answers5

9

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

thebitguru
  • 872
  • 3
  • 12
  • 30
Dzmitry Huba
  • 4,493
  • 20
  • 19
6

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.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
  • You reasoning is correct but I chose to accept another answer because it answers decisively! – Hemant Jul 23 '09 at 10:26
3

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" })



Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • FWIW, Currently my preference is to cache this data rather than store on the page, but this is provided as an option especially when porting a web forms app to mvc and the time for a complete page redesign isn't available. – Adam Tuliper Sep 13 '12 at 16:38
2

Personally I think its obsolete. The only time I've seen ViewState in an ASP.Net MVC app is when someone 'accidentally' added a ASP.Net control to a page.

eyesnz
  • 2,698
  • 2
  • 16
  • 27
0

If you need you can imitate view state with MVC3Futures project. It will let you save the whole model in view.

All you have to do is to serialize model and encrypt it in view.

@Html.Serialize("Transfer", Model, SerializationMode.EncryptedAndSigned)

And in controller add deserialized attribute.

public ActionResult Transfer(string id,[Deserialize(SerializationMode.EncryptedAndSigned)]Transfer transfer)
jan salawa
  • 1,198
  • 1
  • 8
  • 25