写一个Foreach帮助类,在razor中使用
原文发布时间为:2011-05-05 —— 来源于本人的百度文章 [由搬家工具导入]
A Better Razor Foreach Loop(razor delegate extension)
http://haacked.com/archive/2011/04/14/a-better-razor-foreach-loop.aspx
There are many situations where I want to quickly iterate through a bunch of items in a view, and I prefer using the foreach statement. But sometimes, I need to also know the current index. So I wrote an extension method to IEnumerable<T> that accepts Razor syntax as an argument and calls that template for each item in the enumeration.
publicstaticclassHaackHelpers {
publicstaticHelperResult Each<TItem>(
thisIEnumerable<TItem> items,
Func<IndexedItem<TItem>,
HelperResult> template) {
returnnewHelperResult(writer => {
intindex = 0;
foreach(var iteminitems) {
var result = template(newIndexedItem<TItem>(index++, item));
result.WriteTo(writer);
}
});
}
}
This method calls the template for each item in the enumeration, but instead of passing in the item itself, we wrap it in a new class, IndexedItem<T>.
publicclassIndexedItem<TModel> {
publicIndexedItem(intindex, TModel item) {
Index = index;
Item = item;
}
publicintIndex { get;privateset; }
publicTModel Item { get;privateset; }
}
And here’s an example of its usage within a view. Notice that we pass in Razor markup as an argument to the method which gets called for each item. We have access to the direct item and the current index.
@model IEnumerable<Question>
<ol>
@Model.Each(@<li>Item@item.Index of@(Model.Count() - 1):@item.Item.Title</li>)
</ol>
If you want to try it out, I put the code in a package in my personal NuGet feed for my code samples. Just connect NuGet to http://nuget.haacked.com/nuget/ and Install-Package RazorForEach. The package installs this code as source files in App_Code.
写一个Foreach帮助类,在razor中使用的更多相关文章
- 手写一个LRU工具类
LRU概述 LRU算法,即最近最少使用算法.其使用场景非常广泛,像我们日常用的手机的后台应用展示,软件的复制粘贴板等. 本文将基于算法思想手写一个具有LRU算法功能的Java工具类. 结构设计 在插入 ...
- JavaScript写一个表格排序类
依稀记得那是上个星期六的下午,我参加了网易暑期实习生招聘笔试.考得相当糟糕,编程题3个题通过了2个,简答题没做对,选择题貌似是20个题猜了6-7个,99%是挂了,唉唉唉!生活不只眼前的苟且,学习的脚步 ...
- 写一个python脚本监控在linux中的进程
在虚拟机中安装Linux中的CentOS7系统 https://baijiahao.baidu.com/s?id=1597320700700593557&wfr=spider&for= ...
- 如何写一个能在gulp build pipe中任意更改src内容的函数
gulp在前端自动化构建中非常好用,有非常丰富的可以直接拿来使用的plugin,完成我们日常构建工作. 但是万事没有十全十美能够完全满足自己的需求,这时我们就要自己动手写一个小的函数,用于在gulp ...
- .net 在数据访问层中写一个DBhelper优化类
复习了在学校的时候做的WinForm端的一个学生信息管理系统,用的三层架构,看了一下里面的数据优化类 这个类是用来把对数据库的操作封装成静态方法,增删改查的时候直接调用这个类,减少项目里代码的冗余和方 ...
- 编程写一个方法时,注意方法中传参数的数量最好不要超过5个,超过5个怎么办?可以用struct或class,或一个字典类
图 1 一.从图1发现了什么问题呢? 答案:1.参数传的的太多了:2.另外注释也没写好. 说明:一个方法中,传参数的数量最好不要超过5个. 应该采用:struct或class,或一个字典类都行.其中 ...
- 使用Java的多线程和IO流写一个文件复制功能类
创建一个复制功能类,继承Thread类,重写run()方法,把FileInputStream和FileOutputStream输入输出流写在run()方法内.示例代码如下: import java.i ...
- 如何写一个FMDB帮助类?看看runtime吧
FMDB是一个封装很好的sqllite类库.项目中调用的时候只需要写SQL语句,就能实现数据的CURD.我试过即使手写SQL语句也很麻烦,需要一个字段一个字段的拼上去,而且容易出错.有没有动态获取字段 ...
- 最近工作用到压缩,写一个zip压缩工具类
package test; import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream ...
随机推荐
- Too Rich HDU - 5527 (贪心+dfs)
Too Rich Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- [BZOJ] 3875: [Ahoi2014&Jsoi2014]骑士游戏
设\(f[x]\)为彻底杀死\(x\)号怪兽的代价 有转移方程 \[ f[x]=min\{k[x],s[x]+\sum f[v]\} \] 其中\(v\)是\(x\)通过普通攻击分裂出的小怪兽 这个东 ...
- Docker学习笔记--2 镜像的创建
如果我们需要在Docker环境下部署tomcat.redis.mysql.nginx.php等应用服务环境,有下面三种方法: 1,根据系统镜像创建Docker容器,这时容器就相当于是一个虚拟机,进入容 ...
- node操作mogondb数据库的封装
注:摘自网络 上面的注释都挺详细的,我使用到了nodejs的插件mongoose,用mongoose操作mongodb其实蛮方便的. 关于mongoose的安装就是 npm install -g mo ...
- 关于debug
2019-04-05 11:18:15 1. debug 需巧用两个工具 1.1 用‘#’把感觉会出错的代码段注释掉 多行注释有两种快捷操作: 在需要注释的多行代码块前后加一组三引号''' 选中代 ...
- 802. Find Eventual Safe States
https://leetcode.com/problems/find-eventual-safe-states/description/ class Solution { public: vector ...
- 笔记-urllib-parse
笔记-urllib-parse 1. 简介模块官方解释This module defines a standard interface to break Uniform Resource Locato ...
- js:随记
typeof:没有大写,因为typeof是运算符 *1:是转数字 +string:是转数字,在Date对象上是getTime ""+:是转字符串 "":bool ...
- 洛谷P1540 机器翻译
题目链接:https://www.luogu.org/problemnew/show/P1540
- SSH进阶之路
[SSH进阶之路]Hibernate基本原理(一) 在开始学Hibernate之前,一直就有人说:Hibernate并不难,无非是对JDBC进一步封装.一句不难,难道是真的不难还是眼高手低 ...