Asp.net Mvc Framework 5 (pass value to View and Redirect)

Source: Internet
Author: User
The ViewData and TempData attributes are used to pass objects to the View page.
As mentioned above, ViewData can be used to transmit data from the Controller to the View.
In the previous article, we created the EiceController class
In the example in this article, we will change this Controller Namespace MvcApplication2.Controllers
{
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Mvc;
/** // <Summary>
/// Do not remember what I mentioned earlier. All controllers must inherit from
/// Controller class. Of course, there are many Controller types.
/// </Summary>
Public class EiceController: Controller
{
Public void Index (){
ViewData ["ViewData"] = "ViewData is displayed here ";
TempData ["TempData"] = "show TempData" here ";
RenderView ("Index ");
}
Public void Index2 (){
RenderView ("Index ");
}
}
}

We remove the Index parameter and provide values for ViewData and TempData.
In View/Eice/Index. aspx, write the following code: 1: <% = ViewData ["ViewData"] %> <br/>
2: <% = TempData ["TempData"] %>

Note that the above 1.2 is not the row number...

Next we will run the project
Access http: // localhost/Eice/Index
You can see the following:

1. ViewData is displayed here
2. Show TempData here

Access http: // localhost/Eice/Index2 again
The result is 1.
2. Show TempData here

Here 1 shows the content in ViewData, and 2 shows the content passed by TempData.
We can see that
ViewData is valid only in this 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 for temporary cache content or passing error messages when an error page is thrown.
The Redirect method redirects the page to other Controller/Action

RedirectToAction (Action name );
RedirectToAction (Action name, Controller name );
RedirectToAction (RouteValueDictionary );

There is nothing to say About RedirectToAction ("About", "Home") here.
The third type of Overload
You can write

System. Web. Routing. RouteData routeData = new System. Web. Routing. RouteData ();
RouteData. Values. Add ("Action", "About ");
RouteData. Values. Add ("Controller", "Home ");
RedirectToAction (routeData. Values );

In this way, the page Jump can be completed.
Of course, you can also use traditional Response. Redirect to Redirect pages.

Asp.net Mvc Framework Series

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.