博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET controller TO view 数据传递
阅读量:4699 次
发布时间:2019-06-09

本文共 1764 字,大约阅读时间需要 5 分钟。

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

转载于:https://www.cnblogs.com/argenbarbie/p/8080553.html

你可能感兴趣的文章
python生成.exe文件
查看>>
STM32,你了解多少?(转载)
查看>>
用anaconda保证64位和32位的python共存
查看>>
cPanel设置自定义404错误页
查看>>
16.垃圾最小化
查看>>
ROS time stamp and sync
查看>>
将 Shiro 作为应用的权限基础 三:基于注解实现的授权认证过程
查看>>
遍历聚合对象中的元素——迭代器模式(四)
查看>>
Ehab and subtraction(思维题)
查看>>
Codeforces Round 56-C. Mishka and the Last Exam(思维+贪心)
查看>>
统计汉字
查看>>
使用JavaScript重定向URL参数
查看>>
Tomcat系列(5)——Tomcat配置详细部分
查看>>
python生成器
查看>>
Mybatis 面试题
查看>>
Oracle入门《Oracle介绍》第一章1-4 Oracle 用户管理
查看>>
MPEG文件中什么是GOP
查看>>
贪心算法
查看>>
网络侦查与网络扫描
查看>>
循环链表
查看>>