tags:

views:

4864

answers:

4

What is the 'page lifecycle' of an ASP.NET MVC page, compared to ASP.NET WebForms?

I'm tryin to better understand this 'simple' question in order to determine whether or not existing pages I have in a (very) simple site can be easily converted from ASP.NET WebForms.

Either a 'conversion' of the process below, or an alternative lifecycle would be what I'm looking for.

What I'm currently doing:

(yes i know that anyone capable of answering my question already knows all this -- i'm just tryin to get a comparison of the 'lifecycle' so i thought i'd start by filling in what we already all know)

Rendering the page:

  • I have a master page which contains my basic template
  • I have content pages that give me named regions from the master page into which I put content.
  • In an event handler for each content page I load data from the database (mostly read-only).
  • I bind this data to ASP.NET controls representing grids, dropdowns or repeaters. This data all 'lives' inside the HTML generated. Some of it gets into ViewState (but I wont go into that too much!)
  • I set properties or bind data to certain items like Image or TextBox controls on the page.
  • The page gets sent to the client rendered as non-reusable HTML.
  • I try to avoid using ViewState other than what the page needs as a minimum.

Client side (not using ASP.NET AJAX):

  • I may use JQuery and some nasty tricks to find controls on the page and perform operations on them.
  • If the user selects from a dropdown -- a postback is generated which triggers a C# event in my codebehind. This event may go to the database, but whatever it does a completely newly generated HTML page ends up getting sent back to the client.
  • I may use Page.Session to store key value pairs I need to reuse later

So with MVC how does this 'lifecycle' change?

+27  A: 
CMS
+3  A: 

Check this article ASP.NET MVC In-Depth: The Life of an ASP.NET MVC Request

Shiju
interesting article - if a little overwhelming. can anyone comment as to how close the page lifecycle is to the current MVP model (the author states it is for an old preview release and may be out of date).
Simon_Weaver
Its pretty accurate except that now you only call View() instead of RenderView() which has to do with the controller action methods returning ActionResults now. The best way to figure out the specifics is to dig in with Reflector and follow the calls.
Mike Glenn
+6  A: 

I'll attempt to comment on each of the bullet points you mentioned:

You masterpages still exist in MVC and are used to provide a consistent layout to the site. not much new there.

You content pages will become views in the MVC world. They still provide the same content areas to your masterpages.

The eventhandling of webforms should not be used in MVC, instead your Controller classes and their action methods will handle loading your data into a "model" that gets passed to the view.

Although webform style databinding is possible in MVC, I find that it is not the optimal solution. Better to place your data in a model class and strongly type your view so that you have direct access to that model. Then its simply a matter of using the <%= ViewData.Model.SomePropery %> syntax to access your data and display it in the desired locations. As for viewstate, my recommendation is to forget that it even exists.

Remember that one of the advantages of using MVC is that you have control over the HTML you send to the client. Embrace that power and try to find solutions that allow you to maintain that control. Webform controls attempt to hide the the html from you and as such make it more difficult to customize the html when you need to.

I would highly recommend JQuery or one of the other similarly powerful javascript libraries. But learn to use them to access the HTML DOM directly and avoid the id mangling issues of webform controls.

You can use jquery to hook into the dropdown selection on the client side and submit standard or ajax style requests. Those request can return new pages, redirects, html fragments or even JSON data that can be used to update the existing page.

The asp.net Session can be used as needed.

Mike Glenn
thanks for your dtailed answer. it was actually JQuery that drove me to come back to MVC. I'd looked at it briefly and dismissed it (for now at least). playing with JQuery and trying to just find things in the DOM was getting too much of a pain so i thought i'd try to come back to MVC and learn more
Simon_Weaver
A: 

Thank you !! That is my need.

Nguyen Viet Hien