Overview
In the Model-View-controller (MVC) mode, a view is used to encapsulate the presentation logic. These views should not contain any applicationProgramLogical or Database RetrievalCode. All application logic should be processed by the Controller. The view uses the data transmitted from the Controller to present the corresponding UI. This data is usedViewMethod passed to the view from the Controller operation method.
In MIn a common workflow of a VC web application, the Controller operation method processes incoming Web requests.
These operations use input parameter values to execute application code and retrieve or update data model objects in the database. Then, these methods will select a view that presents the response to the browser.
MVC 3.0 view Engine
MVC 3.0 provides two view engines: aspx (C #) and razor (cshtml)
Aspx (C #) Engine
The ASP. net mvc framework allows you to use the view engine to generate a view (UI ). By default, the MVC framework uses the custom types inherited from the existing ASP. NET page (. aspx), master page (. Master), and user control (. ascx) types (Viewpage,ViewmasterpageAndViewusercontrol) As a view.
Razor (cshtml) Engine
_ Appstart. cshtml:
The application is executed after the global. application_start method when it is started.
Content to be processed during app initialization. For example, some information initialized to the database is recorded.
Function and global. application_start is similar. The difference is that the global start is executed first and then to this _ appstart. It is worth noting that it can be used in the _ appstart context. new Dynamic Features of net4 ~~ The Declaration is a type of attribute, field, indexer, parameter, return value, or type constraint.
_ Layout. cshtml:
The layout page is similar to the master page of Aspx.
Create an MVC view (razor engine)
Create an empty MVC 3.0 Application Based on the razor Engine
Select an empty project template and razor view Engine
Name a new controller homecontroller
Right-click the Controller folder
Enter the Controller name. Note that it must end with the Controller.
After adding the controller, the index method is provided by default...
In this case, the view corresponding to the action is not created, so the view is red... An error is reported on the runtime interface ..
PublicActionresult index ()
{
ReturnView ();
}
Let's first output a string to the page...
Code
PublicActionresult index ()
{
ReturnView ();
}
Change
Public StringIndex ()
{
Return "Hello World";
}
After running, a simple interface is displayed...
Add View
At this time, we simply show some strings. However, our interface still requires many elements, not just text ..
So we have to start with the view ..
Return to the previous
PublicActionresult index ()
{
ReturnView ();
}
Modify the previous code to make the Controller index return an actionresult.
For actionresult, see:I want to learn ASP. NET MVC 3.0 (5): Start With Controller/Action
Right-click the action pop-up form and select add view.
We recommend that you do not modify the view name, which will be explained later.
In this case, a view corresponding to the action is generated for the Controller in the view folder.
Compile HTML code in the view
@{
Viewbag. Title = "Homepage ";
}
<H2> <AHref= "Index">Hello World</A> </H2>
Running Effect
Data transmitted between the Controller and view In many cases, we need to pass some data to the interface. Just as the web session code is as follows:
PublicActionresult index ()
{
Viewdata ["Message"]= "Hello World";
ReturnView ();
}
View
@{
Viewbag. Title = "Homepage ";
}
<H2> <AHref= "Index">@ Viewdata ["message"]</A> </H2>
The running effect is still the same...
About tempdata, viewbag, and viewdata
In fact, I personally think they are all the same.
If you call tempdata and viewdata in viewbag mode, the same effect will apply.
And vice versa...
For example, viewbag. Message and viewdata ["message"]
PublicActionresult index ()
{
Viewbag. Message= "Hello World";
//Viewdata ["message"] = "Hello World ";
ReturnView ();
}
The Running Effect of view code remains the same... The effects of repeated debugging are the same ..
Similarities and differences between tempdata, viewdata, and viewbag:
Tempdata viewdata viewbag can be used to save data. The differences between them are as follows: tempdata: stored in the session. Each time the Controller executes a request, it first obtains tempdata from the session, and then clears the session to obtain tempdata, although saved in an internal dictionary object, each entry in its set is deleted from the dictionary table once accessed. At the specific code level, the tempdata acquisition process is to read data from the session of controllercontext through the sessionstatetempdataprovider. loadtempdata method, and then clear the session. Therefore, tempdata can be transferred only once across the controller. Viewdata: the lifecycle is the same as that of the view. It is only valid for the current view. Viewbag: The same as the life cycle of viewdata. It is also valid for the previous view. The difference is that the viewbag type is not the dictionary key-Value Pair structure, but the dynamic type, added in mvc3.
Summary At the beginning, we couldn't make a summary. I will try again later .. (* ^__ ^ *)
Next Preview New razor
Author:Memories of lost youth
Source:Http://www.cnblogs.com/lukun/
The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent andArticleThe original text connection is displayed at an obvious location on the page. If you have any problems, you can useHttp://www.cnblogs.com/lukun/ Thank you very much for contacting me.
Tempdata viewdata viewbag can be used to save data.