asp.net mvc 页面传值的方法总结
转自: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 MVC
Preamble:
In ASP.NET ( like in PHP and other Web frameworks) there are 2 clear entities: Server ( code on the WebServer ) and 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 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 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 done by the browser( how to display html, how to display send file …)
b) Creating/Modifying and send cookies . Browser will do automatically this.
ASP.NET WebForms way:
For ASP.NET Webforms the modalities to transfer are 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 way:
I want to discuss from ASP.NET MVC 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 will discuss of VIEWS – because the VIEWS sends HTML data to the Client.
So, to transfer data between View1 to View2 in MVC is 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 do this:
b1) Return a different View ( using some logic :
if( a )
return View1(Model1);
else
return View2(Model1);
b2) Returning a Redirect to ACTION2 ( that 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.NET MVC is really transfering from ACTION to ACTION , besides the cookie that can be transferred directly by the browser.
9 Modalities to transfer data from Page to Page in ASP.NET MVC
Enough theory, let’s do some code. We have a Model to transfer named ModelTransfer
1
2
3
4
5
6
7
|
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 will server as an example. Also, we will have the more ACTIONS – one for each example of transfer – all are using the TRANSFER action as an ultimate resort do see the View.
Method1 : Transfer directly to the second View/Action .
1
|
< a href = '@Url.Action("Transfer", new { Age = 42, Name = "Andrei Ignat" })' >click me</ a > |
1
|
public ActionResult Transfer(ModelTransfer m) |
Method2 Index sends POST data to a [HttpPost] Index action, that performs some calculations and return a redirect.Usefull in PRG
1
2
3
4
5
|
@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" /> } |
01
02
03
04
05
06
07
08
09
10
|
[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 be transferred to the Transfer view, by TempData
1
|
< a href = '@Url.Action("ServerAction")' >click me</ a > |
1
2
3
4
5
6
7
8
|
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 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
1
|
< a href = '@Url.Action("TransferCookies")' >click me</ a > |
1
2
3
4
5
6
|
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 an entire post by itself and you can see here:
http://msprogrammer.serviciipeweb.ro/2011/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 ASP.NET MVC visit http://asp.net/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, 2012 and is filed under ASP.NET MVC, full. You can follow any responses to this entry through RSS 2.0. You can leave a response, or trackback from your own site.
asp.net mvc 页面传值的方法总结的更多相关文章
- Asp.net mvc页面传值-- dropdownlist
后台传值 List<ConfigParamInfo> paramList = configParamBLL.GetModelList(" and parentID=1" ...
- Asp.net 页面传值的方法
ASP.NET页面传值的方法 From:Refresh-air 在面试的时候,经常会遇到这样的问题,其实我们会对其中的几种方法比较熟悉,因为项目中经常使用.但是要全面的回答ASP.NET中页面传值的方 ...
- ASP.NET页面传值的方法
ASP.NET页面传值的方法 From:Refresh-air 在面试的时候,经常会遇到这样的问题,其实我们会对其中的几种方法比较熟悉,因为项目中经常使用.但是要全面的回答ASP.NET中页面传值的方 ...
- ASP.NET MVC 5 - 验证编辑方法(Edit method)和编辑视图(Edit view)
在本节中,您将验证电影控制器生成的编辑方法(Edit action methods)和视图.但是首先将修改点代码,使得发布日期属性(ReleaseDate)看上去更好.打开Models \ Movie ...
- c#ASP.NET中页面传值共有这么几种方式
一.目前在ASP.NET中页面传值共有这么几种方式: 1.Response.Redirect("http://www.hao123.com",false); 目标页面和原页面可以在 ...
- Asp.Net MVC页面静态化功能实现一:利用IHttpModule,摒弃ResultFilter
上一篇有提到利用IHttpModule和ResultFilter实现页面静态化功能.后来经过一些改动,将ResultFilter中要实现的功能全部转移到IHttpModule中来实现 Asp.Net ...
- Asp.Net MVC页面静态化功能实现一:利用IHttpModule和ResultFilter
由于公司现在所采用的是一套CMS内容管理系统的框架,所以最近项目中有一个需求提到要求实现页面静态化的功能.在网上查询了一些资料和文献,最后采用的是小尾鱼的池塘提供的 利用ResultFilter实现a ...
- ASP.NET中页面传值
一.目前在ASP.NET中页面传值共有这么几种方式: 1.表单提交, <form action= "target.aspx" method = "post&q ...
- Asp.net MVC 权限过滤器实现方法的最佳实践
在项目开发中,为了安全.方便地判断用户是否有访问当前资源(Action)的权限,我们一般通过全局过滤器来实现. Asp.net MVC 页面中常见的权限判断使用过滤器主要在以下几种情况(根据权限判断的 ...
随机推荐
- 机器学习技法-神经网络(NNet)
课程地址:https://class.coursera.org/ntumltwo-002 重要!重要!重要~ 一.神经网络(NNet)的动机 神经网络有很久的历史,由感知机(perceptron)模型 ...
- Bandicam视频录制技巧总结+小丸工具箱压缩视频解决视频体积问题
1.视频录制. 录制质量建议选择100,保证原文件的质量才能更好地保证渲染转码后输出视频的质量.音效这里就一个关键点,就是编码器默认的MPEG-1 L2,会导致会声会影渲染输出出错,程序强行关闭,Ve ...
- UVa 10037 - Bridge
题目大意 在一个晚上有N个人过河,他们有一个手电筒,需要有手电筒才能过河,每次最多两个人同时过河,每次过河时间等于速度最慢的那个人的过河时间,让所有人全部过河,花费的时间最少是多少? 分析 如果只有一 ...
- UVa 1346 - Songs
题目大意:n张唱片,每张都有长度和频率,要求题目中公式值最小.求一个排序,输出该排序下第m张CD的id. 思路:贪心,长度越长放越后面,频率越低放越后面,所以按len/p去排序即可. #include ...
- ZOJ 1016 Parencodings
原题链接 题目大意:有两串数字P和W.数组P中,数字P[i]表示第i个右括号之前的左括号个数.数组W中,数字W[i]表示在第i个右括号和与它匹配的左括号之间的右括号的个数(包括本身).给出一个数组P, ...
- spark之数据源之自动分区推断
在hadoop上创建目录/spark-study/users/gender=male/country=US/users.parquet(并且把文件put上去) code: package cn.spa ...
- Vue.js相关知识2-组件
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Linux定时任务Crontab执行PHP脚本
http://blog.chinaunix.net/uid-7552018-id-182133.html crontab执行php脚本 http://www.jb51.net/article/2913 ...
- A Gentle Introduction to the Gradient Boosting Algorithm for Machine Learning
A Gentle Introduction to the Gradient Boosting Algorithm for Machine Learning by Jason Brownlee on S ...
- caffe: test code Check failed: K_ == new_K (768 vs. 1024) Input size incompatible with inner product parameters.
I0327 20:24:22.966171 20521 net.cpp:849] Copying source layer drop7I0327 20:24:22.966179 20521 net.c ...