MVC匿名类传值学习
2024-10-08 01:12:45
刚接触MVC+EF框架不久,但一直很困惑的就是控制器能否及如何向视图传递匿名类数据。宝宝表示很讨厌去新建实体类啦,查询稍有不同就去建一个实体类不是很麻烦吗,故趁阳光正好,周末睡到自然醒后起来尝试了之前一直在博客园看到的实现方式:英明神武的Tuple类,第一次对微软钦佩之至。故做如下记录,方便自己之后使用。大神就勿喷我啦,宝宝第一次写博客。
首先先描述一下我要实现的功能:从控制器后台查询一些数据,通过匿名类存储,在视图前端遍历输出。初衷实现流程如下:
控制器部分:
private repairsystemEntities db = new repairsystemEntities();
// GET: TEST
public ActionResult Index()
{
var Info = db.bom.ToList().Select(p => Tuple.Create(p.Bom_Brand, p.Bom_Model));
ViewBag.Info = Info;
return View();
}
视图部分:
<table class="table table-hover">
<tbody>
@foreach(var item in ViewBag.Info)
{
<tr>
<td>@(item.Item1)</td>
</tr>
}
</tbody>
</table>
附Tuple类简单说明如下,全部来源于微软官方文档,地址
语法
public static Tuple<T1> Create<T1>(
T1 item1
)
参数
item1
-
Type: T1
元组仅有的分量的值。
返回值
Type: System.Tuple<T1>
元组,其值为 (item1)
使用方法
//类构造函数
var tuple1 = new Tuple<int>();
//helper方法
var tuple2 = Tuple.Create();
//获取值方法直接采用
Console.WriteLine(tuple1.Item1); // Displays 12
Console.WriteLine(tuple2.Item1); // Displays 12
实际例子
// Create a 7-tuple.
var population = new Tuple<string, int, int, int, int, int, int>(
"New York", , ,
, , , );
// Display the first and last elements.
Console.WriteLine("Population of {0} in 2000: {1:N0}",
population.Item1, population.Item7);
// The example displays the following output:
// Population of New York in 2000: 8,008,278
类构造函数创建
// Create a 7-tuple.
var population = Tuple.Create("New York", , , , , , );
// Display the first and last elements.
Console.WriteLine("Population of {0} in 2000: {1:N0}",
population.Item1, population.Item7);
// The example displays the following output:
// Population of New York in 2000: 8,008,278
Create方法
最新文章
- Convert part to feature command
- C++语言-01-简介
- java 15 - 9 集合框架之 栈、队列、数组 和 链表
- mysql 执行状态分析 show processlist
- HTTP 无法注册 URL http://+:9999/CalculatorService/。进程不具有此命名空间的访问权限
- Rechability的简单使用
- 使用md5判断网站内容是否被篡改
- 已知w是一个大于10但不大于1000000的无符号整数,若w是n(n≥2)位的整数,则求出w的后n-1位的数。
- JavaScript中的构造函数
- leetcode水题(一)
- xml文档读取-SAX
- python——常用模块2
- LeetCode之“链表”:Reorder List
- FPGA图像处理之行缓存(linebuffer)的设计一
- HDFS概述
- CTSC2018 被屠记
- 关于js的页面高度和滚动条高度还有元素高度
- 后端开发者的Vue学习之路(一)
- centos6.5修改yum安装的mysql默认目录
- Android Support Library 是什么?
热门文章
- UVA 10900 So you want to be a 2n-aire? (概率dp)
- skynet之伪取消定时器
- CentOS上使用sendmail发送邮件
- centos7没有安装ifconfig命令的解决方法(转)
- Selenium with Python 008 - WebDriver 元素等待
- tracecaller.cs
- laravel中Blade模板继承
- 猪齿鱼_01_环境搭建(二)_微服务支撑组件部署(Docker形式)
- QT中给程序加上主界面的图标
- dubbo常见报错