10

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Goober
  • 13,146
  • 50
  • 126
  • 195
  • See http://stackoverflow.com/questions/366151/asp-net-mvc-doesnt-work-with-viewstate-and-postback – Robert Harvey Aug 16 '09 at 22:55
  • @Robert it might be better to discuss the duplication issue if you disagree than to simply overwrite the edit. – Rex M Aug 16 '09 at 23:07
  • If you have substantial content to add, go ahead. Otherwise, I have already posted a comment about the duplication. – Robert Harvey Aug 16 '09 at 23:08
  • @Robert duplicates are to be closed, not commented on. Please see the FAQ. – Rex M Aug 16 '09 at 23:09
  • The founders have made the purpose of editing clear on their blog. It's intended for substantial edits, not big traffic signs. – Robert Harvey Aug 16 '09 at 23:10
  • @Robert please read the official FAQ on duplicates: http://meta.stackexchange.com/questions/10841/how-to-handle-duplicate-questions/10844#10844 I have followed the format exactly. – Rex M Aug 16 '09 at 23:11
  • Apparently the link you posted is authoritative. http://meta.stackexchange.com/questions/14847/what-makes-an-official-faq-official/14848#14848. So I apologize. – Robert Harvey Aug 16 '09 at 23:53
  • Possible duplicate of [ASP.NET MVC doesn't work with ViewState and Postback?](https://stackoverflow.com/questions/366151/asp-net-mvc-doesnt-work-with-viewstate-and-postback) – Cœur Jul 09 '18 at 16:30

4 Answers4

9

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.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
6

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)
jan salawa
  • 1,198
  • 1
  • 8
  • 25
  • ViewState in ASP.NET is not related to the Model of MVC. ViewState contains the *state* of *controls*; not the values of model objects. – Andrew Barber Mar 20 '13 at 10:35
  • This has been removed in MVC 5 Futures, but you can use MvcSerializer.Serialize instead. See http://stackoverflow.com/questions/25828553/cannot-find-html-serialize-helper-in-mvc-5-futures/26615083#26615083 – Matt Mar 10 '16 at 09:59
5

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.

Saqib A. Azhar
  • 994
  • 1
  • 15
  • 27
0

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

jeef3
  • 2,007
  • 2
  • 17
  • 22