6 ways to pass values to a view by action

Source: Internet
Author: User

When using ASP. Project development, I often encounter problems from action to view, and today I summarize the way I know, divided into the following six kinds:

1. Use ViewData to transmit values

In action, there is the following code: viewdata["name"] = "Vibin1";

You can receive this in the view: Name: @ViewData ["Name"]

Note: ViewData is a simple dictionary whose life cycle is the same as the requested view, only valid for the current view.

2. Use ViewBag to transmit values

In action, there is the following code: viewbag.name = "Vibin2";

You can receive this in the view: Name: @ViewBag. Name

Note : The above viewbag is essentially passed through ViewData, so when ViewData and ViewBag are used at the same time, if the defined variables are the same, the front will be overwritten by the following, as in this article 1, 22 Ways to use together will be displayed: Name: Vibin2. If you do not want to use these two methods of transmission and do not produce overrides, you must ensure that the variable names are different.

3. Use TempData to transmit values

In action, there is the following code: tempdata["name"] = "Vibin3";

You can receive this in the view: Name: @TempData ["Name"]

Note: TempData is saved in session, and this method can be used across the action. The controller fetches the TempData from the session and clears the session each time it is requested, so the data in the TempData can only be passed once by the controller.

4. Use the object as a parameter to return to the view and receive it through the model

In action, you have the following code:

Person p= New Person () {Name = "Vibin4"};

Return View (P);

The person class is defined as follows:

public class Person
{
public string Name {get; set;}
}

You can receive this in the view:

Name: @{person p = Model as person;} @p.name

Note : The namespace of the person class needs to be introduced in the View page, and the person needs to be defined as public. This is also essentially passed through the model object in the ViewData, so for the overload of view (), as long as the parameter with the object model can pass the value, that is, the red box tag overload.

5. You can use the redirect () method, which can be used to pass in a URL, consider a parameter after the URL

In action, there is the following code: Return Redirect ("/another/index?name=vibin5");//another is another controller name

You can receive this in the view: Name: @Request. params["Name"]

Note: because this is a jump, the view is the corresponding view of the action under another controller.

If the URL is this way: "/another/index/vibin5",

You can receive this in the view: Name: @Html. viewcontext.routedata.values["id"]

Similarly, use @request.requestcontext.routedata.values["id"] or @html.viewcontext.routedata.route.getroutedata ( Html.ViewContext.HttpContext). values["id") can also receive a value. This is the default routing rule, the default route is set to: {Controller}/{action}/{id}, the routing rule is not changed, the view receives the variable can only write the ID can not be changed to name.

6. You can use the Redirecttoaction () method, which returns the redirect result object, passing the object object as a parameter in the method

In action, there is the following code: Return redirecttoaction ("Index", "another", new {name= "Vibin6"});//borrow the person class defined previously

You can receive this in the view: Name: @Request. params["Name"]

Note: because this is a jump, the view is the corresponding view of the action under another controller. The Redirecttoaction () method has the following overloads, as long as the parameter with object routevalues can be used to pass the object, see red box identification.

* In addition, the 5th, 6th example, the request.params[for the view to receive URL parameters] can also be changed to request[] or request.querystring[].

6 ways to pass values to a view by action

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.