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();
 }
时间: 2024-08-02 15:12:20

MVC中Controller与View中间的数据传递的常用方法的相关文章

ASP.NET MVC中Controller与View之间的数据传递总结

在ASP.NET MVC中,经常会在Controller与View之间传递数据,因此,熟练.灵活的掌握这两层之间的数据传递方法就非常重要.本文从两个方面进行探讨: Ø Controller向View传递数据 Ø View向Controller传递数据 一.Controller向View传递数据 1.       使用ViewData传递数据 我们在Controller中定义如下: ViewData[“Message”] = “Hello word!”; 然后在View中读取Controller中

ASP.NET MVC中Controller与View之间的数据传递

一.Controller向View传递数据 Controller向View传递数据有3种形式: 1.通过ViewData传递 在Controller里面定义ViewData,并且赋值,比如 ViewData["contact"] = contact; 然后在View里面读取Controller中定义的ViewData数据 比如联系人: <input type="text" value='<%=ViewData["contact"] %

Asp.net MVC中 Controller 与 View之间的数据传递

在ASP.NET MVC中,经常会在Controller与View之间传递数据 1.Controller向View中传递数据 (1)使用ViewData["user"] (2)使用ViewBag.user (3)使用TempData["user"] (4)使用Model(强类型) 区别: (1)ViewData与TempData方式是弱类型的方式传递数据,而使用Model传递数据是强类型的方式. (2)ViewData与TempData是完全不同的数据类型,View

ASP.NET MVC3中Controller与View之间的数据传递总结

</pre>在ASP.NET MVC<span style="font-family:宋体">中,经常会在</span>Controller<span style="font-family:宋体">与</span>View<span style="font-family:宋体">之间传递数据,因此,熟练.灵活的掌握这两层之间的数据传递方法就非常重要.本文从两个方面进行探讨:&

ASP.NET MVC3中Controller与View之间的数据传递

在ASP.NET MVC中,经常会在Controller与View之间传递数据,因此,熟练.灵活的掌握这两层之间的数据传递方法就非常重要.本文从两个方面进行探讨: 一.  Controller向View传递数据 1.       使用ViewData传递数据 我们在Controller中定义如下: [csharp] view plain copy print? ViewData["Message_ViewData"] = " Hello ViewData!"; Vi

Asp.net mvc中controller与view间的如何传递数据

1.      Asp.net中的页面指令 无论是在java程序中还是在.net程序中,我们总是会看见一些@指令,那么这些常见指令的作用是什么呢? ?  @Page指令 只能在.aspx页中使用.如果在其他页面中使用会发生编译错误.比较常见的属性有: 1. Language 指出在编译内联代码块和页的<script>节中出现的所有代码时所使用的语言,默认的语言是Visual Basic .NET. 2. AutoEventWireup 指出是否启用页事件.默认为true. VS.NET开发的页

MVC中Controller与View之间数据互传

转自:http://blog.csdn.net/sknice/article/details/42323791 在ASP.NET MVC中,经常会在Controller与View之间传递数据,因此,熟练.灵活的掌握这两层之间的数据传递方法就非常重要.本文从两个方面进行探讨: 一.Controller向View传递数据 1.使用ViewData传递数据 在Controller中定义如下: ViewData[“Message_ViewData”] = “ Hello ViewData!”; 在Vie

(四)ASP.NET MVC 中 Controller 给 View 传递数据的方式

1. ViewData: 以 ViewData["keyname"] = value 这样键值对的方式进行数据传送.在对应的 cshtml 中用 @ViewData["keyname"] 来获取值. 2. ViewBag: ViewBag 是 dynamic 类型的,是对 ViewData 的一人动态类型封装,用起来更方便,和 ViewData 共同操作一个数据 .在 Controller 中使用 ViewBag.keyname=value 来赋值,在 cshtml

Controller与View之间的数据传递

1)Controller向View传递数据ViewData["message"] = "Hello";//使用ViewData传递数据ViewBag.Time = DateTime.Now;//ViewBag传递数据TempData["mes"] = "text";//使用TempData传递数据//使用静态字段定义变量public static string str;//全局的str = "123";//