I. Passing objects to the view page through viewdata and tempdata attributes
As mentioned aboveViewdataYou canControllerPass to view
In the previous article, we created the eicecontroller class
In the example in this article, we will change this controller
Public ClassEicecontroller: controller {PublicActionresult index () {viewdata ["Viewdata"] ="Viewdata is displayed here"; Tempdata ["Tempdata"] ="Show tempdata here";ReturnView ();}PublicActionresult index2 (){ReturnView ("Index");// The specified view file is the index. aspx in the eice directory.}}
We remove the index parameter and provideViewdataAndTempdataValue assignment
In Views/eice/index. aspx, we write the following:Code
<% @ Page title = "" Language = "C #" masterpagefile = "~ /Views/shared/site. Master "inherits =" system. Web. MVC. viewpage "%> < ASP : Content ID = "Content1" Contentplaceholderid = "Titlecontent" Runat = "Server" > Index </ ASP : Content > < ASP : Content ID = "Content2" Contentplaceholderid = "Maincontent" Runat = "Server" > 1: <% = Viewdata ["viewdata"] %> < BR /> 2: <% = Tempdata ["tempdata"] %> </ ASP : Content >
Note that the above 1.2 is not a row number and is written by me.
Next we will run the project
Access http: // localhost/eice/Index
You can see the following:
1. viewdata2is displayed here. tempdata is displayed here.
Access http: // localhost/eice/index2 again
The result is
. Show tempdata here
Here 1 shows the content in viewdata, and 2 shows the content passed by tempdata.
We can see that viewdata is only valid in the current action.
However, tempdata can still exist on other pages like session, but only one page of access is allowed (similar to flash in monorail)
Tempdata is generally used to pass error messages when temporary cache content or error pages are thrown.
2. Passing objects through viewdata. Model
Create a model: eiceindexmodel. CS.
Public ClassEiceindexmodel {/// <Summary>/// Name/// </Summary>Public StringName {Get;Set;}/// <Summary>/// Gender/// </Summary>Public BoolSex {Get;Set;}}
Then we create a new action: index3
PublicActionresult index3 () {var M =NewEiceindexmodel {name ="ZOU Jian", Sex =True};ReturnView (m );}
Now we create a View File for index3, and right-click index3 and choose addview.
Therefore, a view file is automatically generated, and the result is as follows:
What should we do if we want to display other files?