views:

102

answers:

3

I'm diving into the field of web development after ten years of desktop application development. I'm learning as fast as I can the high level concepts, one of them being MVC. I've noticed things like javascript, css, and html don't fall into the M, V, or the C. I haven't explicitly read it anywhere, but am I right to understand that the MVC pattern is only used to organize code and data on the server side of a website? I apologize if this is a weird question, remember I'm a noob! :)

Thanks in advance for all your help!

+4  A: 

The rendered page can contain javascript, jquery, and other scripting mechanisms. Those things sit squarely in the view, and do all of their work client-side (in the browser).

The rest of it (model and controller) run on the server. Much of the view itself is rendered from the server side.

Here is a small example of a view that groups data together and renders output to the browser.

<ul>
<% foreach (var group in Model.GroupBy(item => item.Category)) { %>

   <li><%= Html.Encode(group.Key) %>
     <ul>

     <% foreach (var item in group) { %>
       <li><%= Html.Encode(item.Data) %></li>  
     <% } %>

     </ul>
   </li>

<% } %>
</ul>

Notice that there is no javascript in there. This code runs entirely from the server. The li and ul tags are passed through to the browser, creating an unordered list of list items.

The output looks something like this in the browser:

Key1
    Data1
    Data2
    Data3
Key2
    Data4
    Data5

..etc.

Note that the code ALL sits on the server, but some of it is executed on the server and some of it (the HTML and Javascript) is passed to the browser and executed there.

Robert Harvey
thanks, robert! another question, what are some things on the server that would fall into the view?
BeachRunnerJoe
@Beeph, template based content generation pages are falls into view category. (for example,jsp (java), aspx(net))
adatapost
+2  A: 

MVC is a way of organizing source code. Where there is source code, you can have any of the three. There's no inherent client or server aspect to any part of the MVC pattern.

For example, recently I implemented a gadget portal in pure javascript (like igoogle). I had a model class to load and save gadget configurations from a json data blob, and to manage the settings of those gadgets. Then I had a view that automatically rendered the gadgets currently loaded into the model based on events sent out by the model. Finally, I had a controller that relayed menu clicks in the rest of the application to update the model. That's MVC, but purely in javascript, and purely client-side.

Joeri Sebrechts
A: 

CSS, HTML, and Javascript deal with the view when using a server-side language that implements the MVC idea. Javascript is a bit muddier since it interacts with the controller more, however.

Karl