Question

I've seen flow charts of the life-cycle but they never seem to answer this question OR I'm just not getting it.

So if user hits a registration page is it:

  1. Routing engine chooses controller.
  2. Controller checks to see which view should be displayed, checks to see if it is strongly typed and if so instantiates the correct model.
  3. Calls the View, passing the empty model in.

I'm also interested in what happens when the form is filled out and submitted.

I'm not looking for anything super technical, just something conceptual...

Was it helpful?

Solution

Regardless of the user actions (followed the link, entered the URL, submitted the form) the basic flow of the MVC application is the following:

  1. According to the routing table Controller's name and method (aka Action) that will handle the request are defined.
  2. If there were any request parameters (values in form for example) they are associated with the Action's parameters.
  3. Request context is generated (contains details about request, client, server, etc.)
  4. Object of Controller's type is created, Action (method) of this object is called with given parameters.
  5. After processing, Action returns an appropriate result, most likely View (could also be Json, plain text, anything).
  6. View is rendered and send back to the client as a response.

Of course, a lot of details are left aside here, but this is the general conception.

Update: some words about models. Models are used to pass data from the Controller to the View. There are two main approaches:

  1. Using ViewData collection - basically a regular key-value dictionary. In Controller it is filled with data:

    ViewData["SomeKey"] = "someValue"; //not only string, any object can be here
    return View();
    

    And in View values are retrieved by keys:

    <%= ViewData["SomeKey"] %>
    
  2. Strongly typed Views. A Model class that will contain necessary data is created. View is specified to be strongly typed with this class, and when Action returns a View object, it passes an instance of this class as a parameter. Here is some code example:

    Model:

    public class SomeModel
    {
        public string SomeKey { get; set; }
    }
    

    Controller:

    SomeModel model = new SomeModel();
    model.SomeKey = "someValue";
    return View(model);
    

    View:

    <%@ Page ... Inherits="System.Web.Mvc.ViewPage<SomeModel>" %>
    ...
    <%= Model.SomeKey %>
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top