I showed you how to pass the values in the controller to the view, but how is it passed? What is the principle?
The view page.cshtml is compiled into a class at compile time, but this class inherits from the Webviewpage<object>
However, the parent class of this class is Webviewpage<object>, but Webviewpage<tmodel> 's parent class Webviewpage
The Webviewpage class also has the four properties of ViewData, ViewBag, TempData, model, but how do you pass this four attribute from the controller class to the four properties of the view class with the same name?
Let's look at the following:
Return a view method in the controller's method, the View method returns the Viewresult,viewresult class and inherits from the Viewresultbase class, we have a Executeresult method in the Viewresultbase class, As we can see, the parameter is the controller context object because the view method has multiple overloads, and if you return directly to view () without any arguments, the system will automatically determine this. ViewName is empty, if empty, then go to routedata inside to get the name of the action is the name of the view.
ViewContext viewcontext=new ViewContext (context,this. View,this. Viewdata,this. Tempdata,output);
As the above method, the controller context, Viewdata,tempdata, is uploaded to ViewContext, which is the view context.
This is then called. View.render (Viewcontext,output); Renders the view context as a parameter, however this. View is a iview interface, so we're going to look at who implemented the iview interface, such as the Razorview and Webformview view engines, to find the Renderview method, In this case, the ViewData data in the controller is assigned to the ViewData, tempdata data of the view view.
MVC Learning Notes (4)