BeachRunnerFred
BeachRunnerFred

Reputation: 18558

For a website, is it true the MVC pattern is only used on the server side?

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!

Upvotes: 2

Views: 811

Answers (4)

Rinat Galyautdinov
Rinat Galyautdinov

Reputation: 255

It's not only to organize the code and the data. With regards to the View: you can do whatever you do with a general web page: jQuery, JavaScript, Ajax, Knockout, etc. And you can make your Ajax talk to your Server side(which is a Controller). MVC works faster than a general aspx web app. It's better for understanding the project/code, and takes less time to introduce the code to the new people in your team. It's better to maintain and with a proper architectual design you won't have to redevelop your prj from a scratch.

Upvotes: 0

Karl
Karl

Reputation: 6165

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.

Upvotes: 0

Joeri Sebrechts
Joeri Sebrechts

Reputation: 11146

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.

Upvotes: 2

Robert Harvey
Robert Harvey

Reputation: 180788

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.

Upvotes: 5

Related Questions