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?

2
  • 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, 2009 at 10:05
  • Related to stackoverflow.com/a/2230635/52277 May 10, 2012 at 21:42

5 Answers 5

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

0
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.

1
  • You reasoning is correct but I chose to accept another answer because it answers decisively!
    – Hemant
    Jul 23, 2009 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" })



1
  • 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. Sep 13, 2012 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.

2
  • 8
    I get your message but since ASP.NET web forms are not obsolete, ViewState is also not obsolete. Its just not used by ASP.NET MVC!
    – Hemant
    Jul 23, 2009 at 10:07
  • It's possible to imitate view state in asp.net mvc
    – jan salawa
    Mar 20, 2013 at 8:28
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.