Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are the MVC and AJAX concepts related?

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.

like image 363
BeachRunnerFred Avatar asked Dec 18 '22 06:12

BeachRunnerFred


1 Answers

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 ;-) )

like image 135
Pascal MARTIN Avatar answered Dec 29 '22 08:12

Pascal MARTIN