300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > asp.net mvc 页面传值的方法总结

asp.net mvc 页面传值的方法总结

时间:2020-03-31 17:49:27

相关推荐

asp.net mvc 页面传值的方法总结

转自:http://msprogrammer.serviciipeweb.ro//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 MVC

Preamble:

In ( like in PHP and other Web frameworks) there are 2 clear entities:Server( code on the WebServer ) andClient( the HTML interpreted by the browser and javascript).

ServerandClientshares samecookies– meansClientandClientboth can read and write cookies.

Transfer from theClienttoServerhappens when

a) you click alink: the information to transfer is query string . That means, http://…/a?x=y&a=b will send information y ( associated to key x) and b( associated to key a). This is called aGET

b) you press asubmitbutton to send aFORM: the information is values of select and input. This is called aPOST.

c) you send information via javascript ( including AJAX) . Usually this can involve aPUT, aGET, or other ( seeREST).

d) Creating/Modifying and send cookies. The sending happens automatically by the browser .

Transfer from theServertoClient

a)sending text(HTML)/binary data. . The interpretation is done by the browser( how to display html, how to display send file …)

b) Creating/Modifying and send cookies . Browser will do automatically this.

WebForms way:

ForWebformsthe modalities to transfer are detailed by Peter Bromberg ,/tutorials/asp-net/e653f028-01fb-4d0e-843b-058deae562a2/eight-different-ways-to-transfer-data-from-one-page-to-another-page.aspx.

MVC way:

I want to discuss fromMVCperspective. InMVCwe have 2 distinct objects:VIEWandACTION. Both happens to run on theServer.

TheACTIONcan return aVIEWor ( or a redirect to) anotherACTIONor simply aFILETheVIEWprocesses a Model ( and a ViewBag/ViewData) and sends the text( HTML) data to theClient .

Instead of PAGES , we will discuss of VIEWS – because the VIEWS sends HTML data to the Client.

So, to transfer data betweenView1toView2in MVC is reduced to this:

a)Page1transfer data to the serverACTION1( by a,b,c,d methods in thePreamble )

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

b1) Return a differentView( using some logic :

if( a )

return View1(Model1);

else

return View2(Model1);

b2) Returning a Redirect toACTION2( that returnView2) or simply return the result of this action

return RedirectToAction(Action2(<parameters>)); //Used in Post/Request/GET,/wiki/Post/Redirect/Get

return Action2(<parameters>);

Resuming: Transfer betweemPAGEtoPAGEinMVCis really transfering fromACTIONtoACTION, besides the cookie that can be transferred directly by the browser.

9 Modalities to transfer data from Page to Page in MVC

Enough theory, let’s do some code. We have a Model to transfer namedModelTransfer

?

We have the firstView1(Index) and a secondView2(Transfer) that will server as an example. Also, we will have the more ACTIONS – one for each example of transfer – all are using theTRANSFERaction as an ultimate resort do see the View.

Method1: Transfer directly to the second View/Action .

? ?

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

? ?

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

? ?

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

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

Method6: No data send. The ServerAction just make some data to be transferred to the Transfer view, by Application

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

Method8: By Cookies

? ?

Method9: By Javascript /Ajax.

It is an entire post by itself and you can see here:

http://msprogrammer.serviciipeweb.ro//12/05/jquery-ajax-request-and-mvcdetailed/

Summary

In this post you have 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” messages to the user.

The code source you will 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. Please learn about T4MVC and Html.EditorFor !

To learn more about MVC visit/mvc.

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

Please do the exercises to gain self knowledge about MVC

This entry was posted on Sunday, January 15th, and is filed MVC,full. You can follow any responses to this entry throughRSS 2.0. You canleave a response, ortrackbackfrom your own site.

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。