Talking about. net MVC, talking about. netmvc
After graduating from college, the concept of MVC is not very clear. I always think that MVC is the same level as the three-layer architecture. In fact, the three-tier architecture is BLL (business logic layer), DAL (database access layer), UI (page display layer), and MVC only belongs to the three-tier architecture UI Layer. It can be said that MVC is to better display data. If we compare the three-tier architecture to a complete person, then MVC is the face of this person and used to display things. To put it so much, people who are new to MVC can understand what MVC is. It is a concept and a mode of displaying data.
Speaking of MVC, I have to say that Microsoft has developed Windows to allow Tom to access computer-related high-tech products long ago, so that ordinary users can replace the previous era of Linux black windows with graphical interfaces, So Microsoft has to say that it has succeeded. Later, Microsoft began to think that it was not a pleasure to turn ordinary users into Tom, but to turn developers into Tom and launch WebForm. I personally felt that this idea was very good and advanced, it may be many years later that we have no "programmers" but designs. However, it is still unrealistic to put it into the present. When I first launched it, I felt very novel and I studied it. I felt very comfortable with Tora, but at that time, the enterprises that used WebForm were very miserable, the reason is that the hidden domain of the page does not know what the gods are, and the performance is poor. The result is very quickly dead (just released from Microsoft. net core does not support WebForm. It can be seen that Microsoft gave up turning programmers into a white hat)
Let's get down to the truth and talk about MVC, M (model), V (view), and C (control ). A model transmits data between a control and a view through an object. A view returned by a control can be transmitted through a model. You can return view (model) in the control method to enable the view to point out the data to be passed through the Model. If you bind the model to the input value, the page to be modified is displayed based on whether the model has data.
In each action, return must be an object inherited from JsonResult, including the most common ViewResult (return View () View, JsonResult (return Json ()) json string, ContentResult (return Content (), etc.
In addition to this type of strong data transmission, control and view can be used to transmit data.
As follows:
1. Use ViewData to pass data
We define the following in Controller (the type is ViewDataDictionary. We can see that this method is suitable for passing key-value pairs ):
ViewData[“Message_ViewData”] = “ Hello ViewData!”;
Then, read the ViewData data defined in the Controller in the View. The Code is as follows:
@Html.Encode(ViewData["Message_ViewData"])
In js, the data in ViewData is read as follows:
<pre name="code" class="javascript"><script type="text/javascript"> var viewData = '@ViewData["Message_ViewData"]'; </script>
2. Use ViewBag to pass data
We define the following in Controller (dynamic can dynamically add its type, and its structure also becomes diversified ):
ViewBag.Message_ViewBag = “ Hello ViewBag !”;
Then read the ViewBag data defined in the Controller in the View. The Code is as follows:
@Html.Encode(ViewBag.Message_ViewBag)
In js, the data in ViewBag is read as follows:
<script type="text/javascript"> var viewBag= '@ViewBag.Message_ViewBag'; </script>
3. Use TempData to transmit data
We define the following in Controller (its type is TempDataDictionary, which is also suitable for transferring key-value pairs. However, this method of data transmission has the following characteristics: there can be only two http requests, once retrieved, it will be destroyed. After research, it is found that the data is stored in the session, and the session will be automatically destroyed once retrieved ):
TempData[“Message”] = “Hello word!”;
Then read the TempData data defined in the Controller in the View. The Code is as follows:
@Html.Encode(TempData["Message_TempData"])
In js, the data in TempData is read as follows:
<script type="text/javascript"> var tempData = '@TempData["Message"]'; </script>