https://stackify.com/viewbag/

In the case of ASP.NET MVC, you have three ways to pass data from the controller to the view. These are

  1. ViewBag,
  2. ViewData and
  3. TempData.

ViewBag and ViewData are highly similar in the way they pass data from controller to view, and both are considered as a way to communicate between the view and the controller within a server call. Both of these objects work well when you use external data.

How does one differ from the other? ViewData is a dictionary or listing of objects that you can use to put data into. The data is now accessible to view. It is based on the ViewDataDictionary class. You can use ViewBag around any ViewData object so that you could assign dynamic properties to it, making it more flexible.

You would need typecasting for ViewData and check for null values, but you do not need to typecast complex data types in ViewBag.

ViewBag vs. TempData

TempData, on the other hand, is also a dictionary and it is based on the TempDataDictionary class. It keeps information temporarily, as long as the HTTP request is active. TempData is perfect for redirects and a few other instances because of its temporary nature.

ViewModel

As discussed in the limitations section, there are several types of data where you cannot use ViewBag, primarily those big and complex data sets. For these types of data, you can use ViewModel if you are using ASP.NET MVC.

ViewModel also has a distinct advantage in that it is strongly typed. This means that unlike in ViewBag, ViewModel does not confuse one type with another type. For example, using ViewBag, the compiler will not detect an error when you use a DateTime as if it were a string.

VIewBag:

1. dynamic

2. not strongly typed

ASP.NET controller TO view 数据传递的更多相关文章

  1. Controller与View数据传递 多Model传递

    1)ViewBag变量方式 使用4个ViewBag变量进行数据传递,Data1.Data2.Data3.Data4的数据直接从数据库里调. Control中伪代码如下所示: 1 public Acti ...

  2. Asp.net Controller中View 和Action方法认证Authorize 及对AuthorizeAttribute扩展

    Asp.net Controller中View和Action方法认证Authorize 在建立Web 站点安全性时 1.登录后才可访问系统文件 ——限制 Forms认证 <authenticat ...

  3. 将Controller中的数据传递到View中显示

    如何将Controller 中的数据传送到View 步骤: (1)要有数据,如果要用到对象可以在Model 中定义对应的类 (2)要有装数据的容器: System.Text.StringBuilder ...

  4. [转]SpringMVC Controller&View数据传递

    Spring MVC3在controller和视图之间传递参数的方法:   一, 从controller往视图传递值, controller---->视图   1)简单类型,如int, Stri ...

  5. MVC进阶学习--View和Controller之间的数据传递(一)

    1.使用ViewData ViewData 的是ControllerBase 的一个属性,是一个数据字典类型的,其实现代码如(这段代码来自asp.net MVC开源项目中源码)下: Code   1  ...

  6. Asp.Net MVC中Controller与View之间传递的Model

    Controller --> View 的Model 与 提交表单后 View --> Controller 的Model 不是相同的对象,即:这两个Model为不同的指针,指向不同的地址 ...

  7. MVC进阶学习--View和Controller之间的数据传递(二)

    1. 使用Request.Form MVC 将页面简单化,与WebForm中的事件机制完全不同,就和普通的html标签表单提交没有任何区别(当然WebForm中的事件机制其实也是表单提交).在表单提交 ...

  8. web开发-前端到服务器Controller中的数据传递

    一, ajax方式 1. ajax获取页面中的数据,包括表单中的数据, 然后封装成对象,数组, 字符串, 或其他基本类型的数据. 2. 将封装得到的数据通过ajax传递到controller中(注:在 ...

  9. ASP.NET-页面间的数据传递

    暑假期间做项目时遇到相关问题,总结如下,与大家分享 1.通过查询字符串传递 这种方式是将参数附加在网址的后面,传递数据简单,但容易暴露,一般用于传递一些简单的数据. 例如,在Default1.aspx ...

随机推荐

  1. Python 全集变量

    1.添加关键字: global 在要给变量从新赋值的时候添加. 全局变量都大写,局部变量都小写.

  2. Jedis使用

    Jedis是java调用Redis的接口. 一.Maven中的jedis依赖包 <!-- https://mvnrepository.com/artifact/redis.clients/jed ...

  3. SPA中,Node路由优先级高于React路由

    一.问题描述 在一场面试中,面试官问到了React和Node路由之间的关系. 现在SPA(单页面应用)的使用越来越广. Node(后台)和React(前端)都有自己的路由,当我页面访问一个URL的时候 ...

  4. 初步接触ES6

    Babel---ES6转码器 ES6 声明变量的六种方法 ES5 只有两种声明变量的方法:var命令和function命令. ES6除了添加let和const命令,另外两种声明变量的方法:import ...

  5. JS代码排版工具

    有时候从网页上下载的js文件删除了空格,看起来就是一整段,还是蛮影响开发效率的,在CSDN上找到一个排版工具,用掉了我最后一个积分,所以在这儿分享一下: 百度云:链接:http://pan.baidu ...

  6. 安装ik分词器以及版本和ES版本的兼容性

    一.查看自己ES的版本号与之对应的IK分词器版本 https://github.com/medcl/elasticsearch-analysis-ik/blob/master/README.md 二. ...

  7. mongdb使用技巧

    进入shell的方法:mongo 命令   # 使用系统服务启动 mongodb /etc/init.d/mongod # 或 service mongod start # 或 service mon ...

  8. Redis的EXPIRE过期机制介绍

    概述在实际开发过程中经常会遇到一些有时效性数据,比如限时优惠活动,缓存或者验证码之类的.过了一段时间就需要删除这些数据.在关系型数据库中一般都要增加一个字段记录数据的到期时间,然后周期性地检查过期数据 ...

  9. overload、override、overwrite的介绍

    答:(1)overload(重载),即函数重载: ①在同一个类中: ②函数名字相同: ③函数参数不同(类型不同.数量不同,两者满足其一即可): ④不以返回值类型不同作为函数重载的条件. (2)over ...

  10. java中\r与\n的区别

    \r : return 到当前行的最左边. \n: newline 向下移动一行,并不移动左右. Linux中\n表示回车+换行: Windows中\r\n表示回车+换行. 测试了一下,在java,w ...