I have posted this question in this forum before ( http://forums.asp.net/t/1365376.aspx ), but as I didn't get any replies I am re-wording it to be a bit more provocative (and I may even slightly overuse CAPITAL LETTERS) in the hope of goading people into replying :-)
Please consider the following quotes (I have added the italics to the parts of the quotes that I refer to below). The first is from the author of a book on ASP.NET and the second is from a Microsoft MVP, in other words, both are from EXPERTS:
http://flux88.com/blog/leveraging-existing-asp-net-features-in-asp-net-mvc/
“As you just discovered, some ASP.NET server controls work with ASP.NET MVC, but which ones? How can we tell if a control will work or not? To put it simply, any control that depends on ViewState or generates post-backs will not work. Some controls will render, but they require a <form runat=”server”> which you might not want to add. Adding a server-side form tag will put hidden fields on the page for ViewState and event validation. The form will also POST to the same action you’re on, which is usually unacceptable. … none of the advanced features of the GridView will work. No sorting, paging, editing, or selecting.”
http://weblogs.asp.net/fredriknormen/archive/2007/11/14/asp-net-mvc-framework-an-early-look.aspx
“The MVC Framework doesn’t support postbacks and the use of ViewState, so most of the Controls shipped with ASP.Net can’t be used. Microsoft will probably add new Controls for the MVC Framework. … Two controls that would be hard to live without would be the <asp:TreeView /> and the <asp:Calendar />. The TreeView works fairly well, however it generates post-backs that do nothing. The visual aspect works just fine, however. The calendar relies heavily on post-back for navigation, so unfortunately it does not function in ASP.NET MVC.”
My response is as follows:
1. The statement that “the MVC Framework doesn’t support postbacks and the use of ViewState” is WRONG. Postbacks do still happen and ViewState still exists under MVC (see further details below).
2. The statement “any control that depends on ViewState or generates post-backs will not work” is WRONG. I have tried a list box with AutoPostBack turned on, as well as a TreeView, GridView and Calendar. The post-backs go back to the controller action that created the view, and the controls all work fully as far as I can tell. The calendar works, and I have successfully used sorting and selecting with the grid – why then do the quotes above INCORRECTLY say that the calendar does not work and that advanced features of the grid such as sorting and selecting do not work?
The only problem I have found is if HTMLHelper.RenderPartial() to render a user control. If this is present on a page, any postback gives the error “Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.” See http://asp-net-controls.blogspot.com/2008/11/renderusercontrol-to-renderpartial.html .
3. If postback is not meant to be used, then why was the strongly typed version of the “AcceptVerbs” attribute introduced as part of MVC (see http://aspalliance.com/1776_ASPNET_MVC_Beta_Released.7 )? This seems to have been introduced specifically to support postbacks, and is used in examples for this purpose (e.g. http://codebetter.com/blogs/david.hayden/archive/2008/11.aspx , which is an example posted by a Microsoft MVP). Why do these examples and the statements of the experts CONTRADICT one another?
4. If we are not meant to use controls such as the grid, what alternative is there? It seems like a big step backwards to have to hand-code the functionality offered by these controls. Is this what ASP.NET MVC XForms ( see http://www.joncurtis.co.uk/post/ASPNet-MVC-XForms.aspx ) is for?
Ok, I know that maybe the experts aren't WRONG, and maybe I'm MISUNDERSTANDING, and hopefully I've annoyed someone enough that they tell me why I'm wrong :-)