Maintaining viewstate in Asp.net mvc?

痴心易碎 提交于 2019-12-17 17:58:41

问题


One of the major reasons for using webforms is the ease of being able to maintain viewstate. I would like to build an asp.net mvc application so what options do I have for maintaining viewstate?

Kind regards


回答1:


ASP.NET MVC does not use ViewState in the traditional sense (that of storing the values of controls in the web page). Rather, the values of the controls are posted to a controller method. Once the controller method has been called, what you do with those values is up to you.

ASP.NET MVC will persist the values of the controls long enough for you to validate them and (if needed) to round-trip them back to your page for editing or correction. If the controls validate, you can persist them to a database or other data store, where they will be available for subsequent GET requests.




回答2:


You can imitate view state by serializing model in view using MVC3Futures project

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)



回答3:


Due to its basic design of maintaining the business layer separate from presentation layer, MVC Framework does not allow to preserve the State over HTTP,

However Cookies, Serializable classes,ViewData and ViewBag are good ways to preserve the state in MVC.




回答4:


If you're wanting to make for example, a Wizard styled form, you could create a Serializable class to retain the viewstate:

[Serializable]
public class MyWizard
{
    public string Field1 { get; set; }
    public string Field2 { get; set; }
}

You could then serialize this class and use it in a similar way to using ViewState (as a hidden field in the form).



来源:https://stackoverflow.com/questions/1285547/maintaining-viewstate-in-asp-net-mvc

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