Summary of methods for ASP. NET MVC page Pass value

Source: Internet
Author: User
Tags send cookies

Transferred from: http://msprogrammer.serviciipeweb.ro/2012/01/15/ Usual-methods-to-transfer-data-from-page-to-page-in-asp-net-mvc/usual methods to transfer data from page to page in ASP. NET mvcpreamble:

In ASP (like in PHP and other WEB frameworks) There is 2 clear entities: Server (code on the WebServer) a nd Client(the HTML interpreted by the browser and JavaScript).

Server and client shares same cookies –means client and client both can Read and write cookies.

Transfer from the Client to Server happens when

A) you click a link : The information-transfer is query string. That means, http://.../a?x=y&a=b would send information Y (associated to key X) and B (associated to key a). This is called a GET

b) You press a submit button to send a FORM : The information is values of select and input. This is called a POST.

c) You send information via JavaScript (including AJAX). Usually this can involve a PUT, a GET, or other (see REST).

d) creating/modifying and send cookies. The sending happens automatically by the browser.

Transfer from the Server to Client

A) sending text (HTML)/binary data. . The interpretation is do by the browser (how to display HTML, how to display send file ...)

b) creating/modifying and send cookies. Browser would do automatically this.

ASP. NET WebForms:

For ASP . Webforms The modalities to transfer is detailed by Peter Bromberg, Http://www.eggheadcafe. com/tutorials/asp-net/e653f028-01fb-4d0e-843b-058deae562a2/ Eight-different-ways-to-transfer-data-from-one-page-to-another-page.aspx.

ASP. NET MVC:

I want to discuss from ASP . Perspective. In MVC we have 2 distinct objects: VIEW and ACTION. Both happens to run on the Server .

    • The action can return a VIEW or (or a redirect to) another action or simply a FILE
    • The VIEW processes a Model (and a viewbag/viewdata) and sends the text (HTML) data to the Client.

Instead of PAGES, we'll discuss of views–because the views sends HTML data to the Client.

So, to transfer data between View1 to View2 in MVC are reduced to this:

A) Page1 transfer data to the server ACTION1(by a,b,c,d methods in the preamble)

b) The Action receives the values as his parameters (by binding) and can does this:

B1) Return a different View (using some logic:

if (a)

Return View1 (MODEL1);

Else

Return View2 (MODEL1);

B2) Returning a Redirect to ACTION2 (so return View2) or simply return the result of this action

Return Redirecttoaction (Action2 (<parameters>)); Used in Post/request/get, Http://en.wikipedia.org/wiki/Post/Redirect/Get

Return Action2 (<parameters>);

Resuming:transfer betweem page to page in asp . Really transfering from action to ACTION , besides the cookie, the can is transferred directly by the browser.

9 Modalities to transfer data from page to page in ASP.

Enough theory, let's do some code. We have a Model to transfer named modeltransfer

?
1234567 public class ModelTransfer    {        public int Age { get; set; }        public string Name { get; set; }    }

We have the first View1( Index) and a second View2(Transfer) that would server as an ex Ample. Also, we'll have the most actions–one for each example of Transfer–all is using the transfer action as an Ultimate Resort do see the View.

Method1 : Transfer directly to the second view/action.

?
1 <ahref=‘@Url.Action("Transfer", new { Age = 42, Name = "Andrei Ignat" })‘>click me</a>
?
1 publicActionResult Transfer(ModelTransfer m)

Method2 Index sends POST data to a [HttpPost] the index action, that performs some calculations and return a redirect. Usefull in PRG

?
12345 @using (Html.BeginForm()) { <input type="text" id="Age" name="Age" value="42" /><input type="text" id="Name" name="Name"value="Andrei Ignat"/><input type="submit" value="Click me" />}
?
01020304050607080910 [HttpPost]       public ActionResult Index(ModelTransfer m)       {           //save to the database the data            //this is for transferring alert data - such an "Completed saving" message to the user            TempData["displayalert"] = " this is from Index POST action!";           //used in PRG            return RedirectToAction("Transfer", new ModelTransfer() { Age = m.Age, Name = m.Name });                   }

Method3: No data send. The serveraction just make some data to being transferred to the Transfer view, by TempData

?
1 <ahref=‘@Url.Action("ServerAction")‘>click me</a>
?
12345678 public ActionResult ServerAction()       {           //You can put also into the Session / Application /Cache depending on your specifications           TempData["MyModel"]=new ModelTransfer(){ Age = 42, Name = "Andrei Ignat"};           TempData["displayalert"] = "this is from Server action!";                      return RedirectToAction("Transfer");       }

Method4: No data send. The serveraction just make some data to is transferred to the Transfer view, by Cache

Method5: No data send. The serveraction just make some data to is transferred to the Transfer view, by Session

Method6: No data send. The serveraction just make some data to being transferred to the Transfer view, by application

Method7: No data send. The serveraction just make some data to is transferred to the Transfer view, by HttpContext Items

Method8: by Cookies

?
1 <ahref=‘@Url.Action("TransferCookies")‘>click me</a>
?
123456 HttpCookie cook = new HttpCookie("Transfer");           //usually you put here more , but now I do not want to interfere with other methods           cook.Expires = DateTime.Now.AddSeconds(1);           cook.Value = "from transfer cookies";           Response.Cookies.Add(cook);           return RedirectToAction("Transfer");

Method9: by Javascript/ajax.
It is a entire post by itself and you can see here:
http://msprogrammer.serviciipeweb.ro/2011/12/05/jquery-ajax-request-and-mvcdetailed/

Summary

The This post is seen 9 methods to transfer data in MVC. As a bonus, the page dispolays also a message with Javascript (usefull for messaging like "Data Saved to database" message s to the user.
The code source you'll find here:

Transfer Data page to page
It is made with Razor and mvc3–but you can replace Razor with ASPX and MVC3 with MVC2 also.

If you think I can improve this post, please leave some comment.

Notes:

I used here hard coding values. Learn about T4MVC and html.editorfor!

To learn more about ASP. NET MVC visit Http://asp.net/mvc.

Default Tempdataprovider is based on Session. There is one more, based on cookies.

Exercises to gain self knowledge about MVC

This entry were posted on Sunday, January 15th, + and is filed under ASP, full. You can follow any responses to this entry through RSS 2.0. You can leave a response, or trackback from your own site.

Summary of methods for ASP. NET MVC page Pass value

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.