ASP. net mvc key tutorial first anniversary edition fourth back to view pass value

Source: Internet
Author: User
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?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.