Reputation: 59
I am working on MVC application and trying to understand following:
From MVC controller I am populating model and sending it to view, view has JQuery, razor code and HTML elements, which will be created first? HTML will create DOM and than JQuery run and than raor code?
Model has all validation and value(text) for all the lables, so razor code has to execute first, right?
I read it somewhere that "what will be executed first?" is depend on where you write JQuery code, in header OR at the end of body? I don't understand this..
Upvotes: 1
Views: 563
Reputation: 337560
From MVC controller I am populating model and sending it to view, view has JQuery, razor code and HTML elements, which will be created first? HTML will create DOM and than JQuery run and than razor code?
Razor runs on the server side and is used in ASP.Net MVC Views to create the HTML, which will then be sent to the client. It's then down to the receiving browser to generate the DOM and finally run any JS.
Model has all validation and value(text) for all the labels, so razor code has to execute first, right?
No. The ModelBinder executes and validates the model (assuming you've setup some Data Annotations) first. Razor is only used when the View is being output, which is one of the final steps in the server-side pipeline.
I read it somewhere that "what will be executed first?" is depend on where you write JQuery code, in header OR at the end of body? I don't understand this..
That's relating solely to client-side logic. Best practices are to place JS/jQuery code in either the <head>
of the page or just before the </body>
. If you do the former you will need a method of waiting until the DOM loads before you execute your JS. In plain JS this would involve adding a DOMContentLoaded
event handler to the window
. In jQuery it would be a document.ready event handler.
In short, Razor and jQuery run on different sides of the client/server divide, at completely different times. They are entirely unrelated to each other.
Upvotes: 1