views:

168

answers:

3

Hello! I'm currently diving into web development after ten years of desktop development and I'm trying to get a high level grasp on the many concepts I'm learning. The two most recent concepts I've been reading up on are MVC (specifically ASP.NET MVC) and AJAX. I understand MVC is a pattern used to separate logic and data and AJAX is a combination of various web technologies for creating asynchronous and dynamic web pages.

  1. How are the two related?
  2. Can or should the two be used together?
  3. If so, can you give some simple examples?

I apologize if these are strange questions and I'm comparing apples to oranges, forgive me as I'm still a huge gigantic noob :)

Thanks in advance for all your help!

+6  A: 

Ajax is just of way of requesting data : generally, with Ajax, instead of requesting a full HTML webpage, you just request :

  • either a part of the page (say, the HTML code of one part of the screen you want to refresh without reloading the whole page)
  • or some data ; using JSON or XML as data-exchange format, for instance

MVC describes the stacks used to :

  • Access the data and do actions / calculations / whatever on it (M)
  • Present it (V)
  • Going through the Controller, which determine which Model and View should be used to serve the data you requested.

When you use an Ajax request, you do exactly as you'd do serving a full page :

  • get a request
  • determine which Model and method should be called
  • call them (maybe they'll do something with a Database, or whatever they have to)
  • pass the data to the View, which will render it

The two differences are :

  • The "View", in one case, renders a full HTML page : in the other case, only a part of it, or some JSON / XML format
  • In one case, the request is generally done in asynchronous mode

Ajax or not, you are free to use MVC... or not !
If you are using MVC for non-Ajax request, then, why not do so for Ajax requests too ?

Sorry, I won't give any example of code - I'm not a .NET developer, so won't be able to help with that (but the concept is the same in other languages ;-) )

Pascal MARTIN
"Ajax is just of way of requesting data : generally, with Ajax" - I think you mean XHR for that second "Ajax"
David Dorward
Indeed, yes I do ^^ (falling myself in the kind of language-abuse -- if this can be said in english -- that I sometimes fight against :-D )
Pascal MARTIN
+4  A: 

I'll bite...

  1. They're not related in any way other than they're both web concepts. Ajax is a method of performing an HTTP request and handling the result. MVC is an architectural pattern for building your web application.

  2. Yes. No matter what architecture you use for your web apps, you should use Ajax where it makes sense. Ajax can increase the performance of your app, and improve the user experience and UI by eliminating bloated data transfer and screen flickering/refresh.

  3. You can use Ajax easily through a number of javascript frameworks such as JQuery. Simply download and include the framework .js files in your application, reference it from your html, and read the documentation on how to make Ajax requests.

womp
+2  A: 

How are they related?

I'm assuming you're taking this from the POV of an ASP.NET dev.

It's really just because plain Ajax (+JSON) works much better with ASP.NET MVC than ASP.NET Webforms. Because of the way ASP.NET Webforms muddled together the view and the controller (and not to mention having Postback & Viewstate -- its crutch to allow pre-Ajax pages to overcome web statelessness), this was initially very difficult.

The way ASP.NET server controls cannot be directly (or at least easily) accessed from Javascript practically made it impossible to use conventional Ajax until ASP.NET Ajax UpdatePanels came along (which was, IMHO, another crutch).

With the advent of ASP.NET MVC it's now much easier to use straight Ajax libraries and JSON for ASP.NET web applications.

Jon Limjap