Is ViewState relevant in ASP.NET MVC?

℡╲_俬逩灬. 提交于 2020-01-22 17:03:27

问题


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?


回答1:


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




回答2:


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.




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






回答4:


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.




回答5:


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)


来源:https://stackoverflow.com/questions/1170699/is-viewstate-relevant-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!