博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ViewBag和ViewData
阅读量:4962 次
发布时间:2019-06-12

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

作者: 
出处: 
 
ViewData ViewBag
它是Key/Value字典集合 它是类型对像
从Asp.net MVC 1 就有了 ASP.NET MVC3 才有
基于Asp.net 3.5 framework 基于Asp.net 4.0与.net framework
ViewData比ViewBag快 ViewBag比ViewData慢
在ViewPage中查询数据时需要转换合适的类型 在ViewPage中查询数据时不需要类型转换
有一些类型转换代码 可读性更好

 

 

 

 

 

 

 

 

 

在Controller中使用ViewData:

public ActionResult UsingViewData(){        ViewData["Title"] = " Using ViewData";    ViewData["ProjectName"] = "My Test Project";    ViewData["ProjectDescription"] = "This is Test Project to demo Viewdata and viewbag details";    ViewData["StartDate"] = new DateTime(2011, 1, 1);    ViewData["TotalPrice"] = 1000;    ViewData["TotalDays"] = 100;    Dictionary
stackholder = new Dictionary
(); stackholder.Add("Client","Mr. Client"); stackholder.Add("Manager","Mr. Joy"); stackholder.Add("Team Leader","Mr.Toy"); stackholder.Add("Sr. developer", "Mr.dojoy"); stackholder.Add("developer", "Mr. nodoy"); ViewData["stackholder"] = stackholder; List
modules = new List
(); modules.Add("Admin module"); modules.Add("ShoppingCart module"); modules.Add("CMS module"); ViewData["modules"] = modules; return View();}

对应UsingViewData View的cshtml:

@ViewData["Title"]

Project Name : @ViewData["ProjectName"]

ProjectDescription :

"@ViewData["ProjectDescription"]".

Stack Holder :
    @foreach ( var stakerholder in ViewData["stackholder"] as Dictionary
    ) {
  • @stakerholder.Key   : @stakerholder.Value
  • }
Project Details:
module List :
    @foreach ( var module in ViewData["modules"] as List
    ) {
  • @module
  • }
Project StartDate : @ViewData["StartDate"]
Project TotalPrice: @ViewData["TotalPrice"]
Project TotaDays : @ViewData["TotalDays"]

然后是ViewBag:

public ActionResult UsingViewBag(){    ViewBag.Title = " Using ViewBag";    ViewBag.ProjectName = "My Test Project";    ViewBag.ProjectDescription = "This is Test Project to demo Viewdata and viewbag details";    ViewBag.StartDate = new DateTime(2011, 1, 1);    ViewBag.TotalPrice = 1000;    ViewBag.TotalDays = 100;    Dictionary
stackholder = new Dictionary
(); stackholder.Add("Client", "Mr. Client"); stackholder.Add("Manager", "Mr. Joy"); stackholder.Add("Team Leader", "Mr.Toy"); stackholder.Add("Sr. developer", "Mr.dojoy"); stackholder.Add("developer", "Mr. nodoy"); ViewBag.stackholder = stackholder; List
modules = new List
(); modules.Add("Admin module"); modules.Add("ShoppingCart module"); modules.Add("CMS module"); ViewBag.modules = modules; return View();}

对应View UsingViewBag 的cshtml的ViewBag:

@ViewBag.Title

Project Name : @ViewBag.ProjectName

ProjectDescription :

"@ViewBag.ProjectDescription.

Stack Holder :
    @foreach ( var stakerholder in ViewBag.stackholder ) {
  • @stakerholder.Key   : @stakerholder.Value
  • }
Project Details:
module List :
    @foreach ( var module in ViewBag.modules ) {
  • @module
  • }
Project StartDate : @ViewBag.StartDate.ToString("dd-MMM-yyyy")
Project TotalPrice: @ViewBag.TotalPrice ₹
Project TotaDays : @ViewBag.TotalDays

后面是在Controller中使用ViewBag:

public ActionResult UsingViewBagInControlAndViewDataInView(){    ViewBag.Title = " Using ViewBag In Control And ViewData In View";    ViewBag.ProjectName = "My Test Project";    ViewBag.ProjectDescription = "This is Test Project to demo Viewdata and viewbag details";    ViewBag.StartDate = new DateTime(2011, 1, 1);    ViewBag.TotalPrice = 1000;    ViewBag.TotalDays = 100;    Dictionary
stackholder = new Dictionary
(); stackholder.Add("Client", "Mr. Client"); stackholder.Add("Manager", "Mr. Joy"); stackholder.Add("Team Leader", "Mr.Toy"); stackholder.Add("Sr. developer", "Mr.dojoy"); stackholder.Add("developer", "Mr. nodoy"); ViewBag.stackholder = stackholder; List
modules = new List
(); modules.Add("Admin module"); modules.Add("ShoppingCart module"); modules.Add("CMS module"); ViewBag.modules = modules; return View();}

在UsingViewBagInControlAndViewDataInView.cshtml中使用ViewData来取值:

@ViewData["Title"]

Project Name : @ViewData["ProjectName"]

ProjectDescription :

"@ViewData["ProjectDescription"]".

Stack Holder :
    @foreach ( var stakerholder in ViewData["stackholder"] as Dictionary
    ) {
  • @stakerholder.Key   : @stakerholder.Value
  • }
Project Details:
module List :
    @foreach ( var module in ViewData["modules"] as List
    ) {
  • @module
  • }
Project StartDate : @ViewData["StartDate"]
Project TotalPrice: @ViewData["TotalPrice"]
Project TotaDays : @ViewData["TotalDays"]

反过来是这样的: 

public ActionResult UsingViewDataInControlAndViewBagInView(){    ViewData["Title"] = " Using ViewData In Control And ViewBag In View";    ViewData["ProjectName"] = "My Test Project";    ViewData["ProjectDescription"] = "This is Test Project to demo Viewdata and viewbag details";    ViewData["StartDate"] = new DateTime(2011, 1, 1);    ViewData["TotalPrice"] = 1000;    ViewData["TotalDays"] = 100;    Dictionary
stackholder = new Dictionary
(); stackholder.Add("Client", "Mr. Client"); stackholder.Add("Manager", "Mr. Joy"); stackholder.Add("Team Leader", "Mr.Toy"); stackholder.Add("Sr. developer", "Mr.dojoy"); stackholder.Add("developer", "Mr. nodoy"); ViewData["stackholder"] = stackholder; List
modules = new List
(); modules.Add("Admin module"); modules.Add("ShoppingCart module"); modules.Add("CMS module"); ViewData["modules"] = modules; return View();}

在UsingViewDataInControlAndViewBagInView.cshtml 的View中使用ViewBag来取值:

@ViewBag.Title

Project Name : @ViewBag.ProjectName

ProjectDescription :

"@ViewBag.ProjectDescription.

Stack Holder :
    @foreach ( var stakerholder in ViewBag.stackholder ) {
  • @stakerholder.Key   : @stakerholder.Value
  • }
Project Details:
module List :
    @foreach ( var module in ViewBag.modules ) {
  • @module
  • }
Project StartDate : @ViewBag.StartDate.ToString("dd-MMM-yyyy")
Project TotalPrice: @ViewBag.TotalPrice ₹
Project TotaDays : @ViewBag.TotalDays

这样对比看上去会比较清楚一些。在源代码中的ViewBag是这样的属性:

public dynamic ViewBag {    get {        if (_dynamicViewData == null) {            _dynamicViewData = new DynamicViewDataDictionary(() => ViewData);        }        return _dynamicViewData;    }}

ViewData是:

public ViewDataDictionary ViewData {    get {        if (_viewData == null) {            SetViewData(new ViewDataDictionary());        }        return _viewData;    }    set {        SetViewData(value);    }}

 

 

转载于:https://www.cnblogs.com/2260827114com/p/7076886.html

你可能感兴趣的文章
mysql备份与恢复
查看>>
混沌分形之迭代函数系统(IFS)
查看>>
边框圆角Css
查看>>
使用Busybox制作根文件系统
查看>>
jpg图片在IE6、IE7和IE8下不显示解决办法
查看>>
delphi之模糊找图
查看>>
Javascript模块化编程的写法
查看>>
大华门禁SDK二次开发(二)-SignalR应用
查看>>
oracle 使用job定时自动重置sequence
查看>>
集成百度推送
查看>>
在项目中加入其他样式
查看>>
在使用Kettle的集群排序中 Carte的设定——(基于Windows)
查看>>
【原】iOS中KVC和KVO的区别
查看>>
OMAPL138学习----DSPLINK DEMO解析之SCALE
查看>>
IoC的基本概念
查看>>
restframework CBV试图的4种方式
查看>>
大图居中,以1920px为例
查看>>
[C陷阱和缺陷] 第7章 可移植性缺陷
查看>>
linux中configure文件默认执行结果所在位置
查看>>
Windows向Linux上传文件夹
查看>>