ViewBag

在MVC3開始。视图数据能够通过ViewBag属性訪问。在MVC2中则是使用ViewData。MVC3中保留了ViewData的使用。ViewBag 是动态类型(dynamic),ViewData 是一个字典型的(Dictionary)。

ViewBag和ViewData的差别:

ViewBag 不再是字典的键值对结构。而是 dynamic 动态类型。它会在程序执行的时候动态解析。

所以在视图中获取它的数据时候不须要进行类型转换

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

Contorller

<pre class="csharp" name="code">using NewOjbect.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace NewOjbect.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.UserName = "无盐海";
ViewBag.Age = "25";
ViewBag.Gender = 1; string[] Itmes = new string[] { "中国", "美国", "德国" };
ViewBag.itemsA = Itmes;// viewbag是一个新的dynamic类型keyword的封装器 //ViewData["Items"] = items; return View();
}
}
}

View

<pre class="html" name="code"><html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Test2</title>
</head>
<body>
<div>
username:<input type="text" id="UserName" name="UserName" value="@ViewBag.UserName" /></br> 年 龄: <input type="text" id="age" name="age" value=@ViewBag.Age /></br> 性 别:<input type="text" id="Gender" name="Gender" value="@ViewBag.Gender" /></br> <button>提交</button> <!---这里输出国家名-->>
@foreach (dynamic item in ViewBag.itemsA)
{
<p>@item</p>
}
</div>
</body>
</html>



出处:https://www.cnblogs.com/cynchanpin/p/7065098.html

ViewBag+Hashtable应用例子

ViewBag+Hashtable应用例子

ViewBag+Hashtable应用例子

1.Controller返回Hashtable 数据


public ActionResult Index()
{
ViewBag.Qual = getQual();//返回ViewBag用Hashtable格式
return View();
} public Hashtable getQual()
{
var sql = "select Qualification, Qualification_url from C_Qualification";
var Qual_dt = DbHelperSQL.Query(sql);//查询数据后返回datatable数据
Hashtable hash = new Hashtable();//new一个Hashtable对象
for (int i = 0; i < Qual_dt.Rows.Count; i++)
//根据datatable数据行数遍历再添加到Hashtable以键值对形式存在:::key:value
{
hash.Add(Qual_dt.Rows[i][0].ToString(), Qual_dt.Rows[i][1].ToString());
}
return hash;
}

2.前端显示及Hashtable 数据foreach遍历

                    <div class="layui-tab-item">
<table class="layui-table">
<colgroup>
<col width="150">
<col width="200">
<col>
</colgroup>
<thead>
<tr>
<th>资质名称</th>
<th>预览</th>
</tr>
</thead> <tbody> @{
foreach (var item in ViewBag.Qual)
{
<tr>
<td>@item.Key</td>
<td>@item.Value</td>
</tr>
} }
</tbody>
</table>
</div>



ViewBag---MVC3中 ViewBag、ViewData和TempData的使用和差别-------与ViewBag+Hashtable应用例子的更多相关文章

  1. ASP.NET MVC4中ViewBag、ViewData和TempData的使用和区别

    一.说明 本文章主要是讲解asp.net mvc中ViewBag.ViewData和TempData的使用和区别,ViewBag.ViewData和TempData常常用于将action方法中的数据传 ...

  2. MVC3中 ViewBag、ViewData和TempData的使用和区别

    在MVC3开始,视图数据可以通过ViewBag属性访问,在MVC2中则是使用ViewData.MVC3中保留了ViewData的使用.ViewBag 是动态类型(dynamic),ViewData 是 ...

  3. MVC3+中 ViewBag、ViewData和TempData的使用和区别

    在MVC3开始,视图数据可以通过ViewBag属性访问,在MVC2中则是使用ViewData.MVC3中保留了ViewData的使用.ViewBag 是动态类型(dynamic),ViewData 是 ...

  4. MVC3中 ViewBag、ViewData和TempData的使用和区别(不是自己写的)

    (网上抄的,并未消化)在MVC3开始,视图数据可以通过ViewBag属性访问,在MVC2中则是使用ViewData.MVC3中保留了ViewData的使用.ViewBag 是动态类型(dynamic) ...

  5. MVC3中 ViewBag、ViewData和TempData的使用和区别(转发:汴蓝)

    MVC3中 ViewBag.ViewData和TempData的使用和区别   在MVC3开始,视图数据可以通过ViewBag属性访问,在MVC2中则是使用ViewData.MVC3中保留了ViewD ...

  6. MVC3中 ViewBag、ViewData和TempData的使用和区别(转载)

    在MVC3开始,视图数据可以通过ViewBag属性访问,在MVC2中则是使用ViewData.MVC3中保留了ViewData的使用.ViewBag 是动态类型(dynamic),ViewData 是 ...

  7. MVC MVC3中 ViewBag、ViewData和TempData的使用和区别 【转】

    在MVC3开始,视图数据可以通过ViewBag属性访问,在MVC2中则是使用ViewData.MVC3中保留了ViewData的使用.ViewBag 是动态类型(dynamic),ViewData 是 ...

  8. MVC中 ViewBag、ViewData和TempData区别

    MVC3中 ViewBag.ViewData和TempData的使用和区别 public dynamic ViewBag { get; } public ViewDataDictionary View ...

  9. 浅谈 MVC中的ViewData、ViewBag和TempData

    ViewBag和TempData的区别 ViewData ViewBag 它是Key/Value字典集合 它是dynamic类型对像 从Asp.net MVC 1 就有了 ASP.NET MVC3 才 ...

随机推荐

  1. Nginx正向代理设置

    Nginx不仅可以做反向代理,实现负载均衡.还能用作正向代理来进行上网等功能. 正向代理:如果把局域网外的Internet想象成一个巨大的资源库,则局域网中的客户端要访问Internet,则需要通过代 ...

  2. [Bayes] MCMC (Markov Chain Monte Carlo)

    不错的文章:LDA-math-MCMC 和 Gibbs Sampling 可作为精进MCMC抽样方法的学习材料. 简单概率分布的模拟 Box-Muller变换原理详解 本质上来说,计算机只能生产符合均 ...

  3. shell 数学计算的N个方法

    let使用方法 root@172-18-21-195:/tmp# n1=5 root@172-18-21-195:/tmp# n2=10 root@172-18-21-195:/tmp# let re ...

  4. EasyNVR智能云终端硬件盒子x86版自我维护之摄像机网页直播系统基础运维

    背景分析 随着EasyNVR软件为越来越多的用户接受和使用,我们也致力于用户的需求收集和需求的调研,发现一部分用户有关于硬件设备的需求,加之我们推出的免费产品EasyNVS云管理平台,可以说用户自己搭 ...

  5. CenterOS7 安装Mysql8 及安装会遇到的问题

    1.下载 MySQL 所需要的安装包 网址:https://dev.mysql.com/downloads/mysql/ 2.Select Operating System: 选择 Red Hat , ...

  6. 【神经网络与深度学习】【计算机视觉】YOLO2

    YOLO2 转自:https://zhuanlan.zhihu.com/p/25167153?refer=xiaoleimlnote 本文是对 YOLO9000: Better, Faster, St ...

  7. FPGA程序编译后逻辑单元数为0

    问题 FPGA代码写完后编译不报错,但是显示使用的逻辑单元数(Total logic elements)为0.当然程序也不工作. 我用的是Intel Altera FPGA,verilog语言,在Qu ...

  8. javascript添加到收藏夹写法

    javascript添加到收藏夹写法 <pre>function addFavorite2() { var url = window.location; var title = docum ...

  9. VyOS 关于dhcp server 和dhcp relay 切换需要注意的

    dhcp server : /config/dhcpd.leases dhcp relay 两个dhcp 模式切换需要删除这个配置文件

  10. JavaScript原生封装ajax请求和Jquery中的ajax请求

    前言:ajax的神奇之处在于JavaScript 可在不重载页面的情况与 Web 服务器交换数据,即在不需要刷新页面的情况下,就可以产生局部刷新的效果.Ajax 在浏览器与 Web 服务器之间使用异步 ...