MVC中Controller与View中间的数据传递的常用方法

原文: MVC中Controller与View中间的数据传递的常用方法

这几天正在学习MVC,顺便就将自己每天的学习心得记录下来与大家分享一下吧!

在MVC中,Controller与View之间传递数据是很频繁的事情,所以在这里就总结一下我自己在学习中使用的几种常用的方法:

 

将数据从Controller中传递到View中:

ViewData:它是Key/Value字典集合

赋值方式(cs文件中):

ViewData["Demo"]="Hello world!";

使用方法(aspx文件中):

  <h1>
            <%:ViewData["demo"] %>
  </h1>

 

将数据从View中传递到Controller中:

方法一:使用FormCollection类型参数获取数据

 public ActionResult Index(FormCollection collection)
 {
    string str = collection["demo"];    //str="hello world!"
       return View();
 }

方法二:使用Request[key]获取数据

 public ActionResult Index()
 {
  string str = Request["demo"];    //str="hello world!"
    return View();
 }

 

posted on 2018-08-02 23:53 NET未来之路 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/9410943.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值