原文发布时间为:2011-04-27 —— 来源于本人的百度文章 [由搬家工具导入]

Templated Razor Delegates

David Fowler turned me on to a really cool feature of Razor I hadn’t realized made it into 1.0, Templated Razor Delegates. What’s that? I’ll let the code do the speaking.

@{
Func<dynamic, object> b = @<strong>@item</strong>;
}
<span>This sentence is @b("In Bold").</span>

That could come in handy if you have friends who’ll jump on your case for using the bold tag instead of the strong tag because it’s “not semantic”. Yeah, I’m looking at you Damian . I mean, don’t both words signify being forceful? I digress.

Note that the delegate that’s generated is a Func<T, HelperResult>. Also, the @item parameter is a special magic parameter. These delegates are only allowed one such parameter, but the template can call into that parameter as many times as it needs.

The example I showed is pretty trivial. I know what you’re thinking. Why not use a helper? Show me an example where this is really useful. Ok, you got it!

Suppose I wrote this really cool HTML helper method for generating any kind of list.

publicstaticclassRazorExtensions {
publicstaticHelperResult List<T>(thisIEnumerable<T> items,
Func<T, HelperResult> template) {
returnnewHelperResult(writer => {
foreach(var iteminitems) {
template(item).WriteTo(writer);
}
});
}
}

This List method accepts a templated Razor delegate, so we can call it like so.

@{
var items = new[] { "one", "two", "three" };
}

<ul>
@items.List(@<li>@item</li>)
</ul>

As I mentioned earlier, notice that the argument to this method, @<li>@item</li> is automatically converted into a Func<dynamic, HelperResult> which is what our method requires.

Now this List method is very reusable. Let’s use it to generate a table of comic books.

@{
var comics = new[] {
new ComicBook {Title = "Groo", Publisher = "Dark Horse Comics"},
new ComicBook {Title = "Spiderman", Publisher = "Marvel"}
};
}

<table>
@comics.List(
@<tr>
<td>@item.Title</td>
<td>@item.Publisher</td>
</tr>)
</table>

This feature was originally implemented to support the WebGrid helper method, but I’m sure you’ll think of other creative ways to take advantage of it.

If you’re interested in how this feature works under the covers, check out this blog post by Andrew Nurse.

http://haacked.com/archive/2011/02/27/templated-razor-delegates.aspx

转载于:https://www.cnblogs.com/handboy/p/7163978.html

RazorExtensions Templated Razor Delegates相关推荐

  1. asp.net mvc3 Razor引擎中@使用规则小记

    项目中前台用的是asp.net mvc3,Razor引擎(关于Razor的介绍可以参考:http://weblogs.asp.net/scottgu/archive/2010/07/02/introd ...

  2. razor html帮助器,在 ASP.NET 网页(Razor)网站中创建和使用帮助器 | Microsoft Docs

    在 ASP.NET 网页(Razor)网站中创建和使用 Helper 02/17/2014 本文内容 本文介绍如何在 ASP.NET 网页(Razor)网站中创建帮助程序. 帮助器是一种可重用的组件, ...

  3. Razor:从aspx到cshtml常见错误及正确书写方法

    http://blog.csdn.net/cheny_com/article/details/6298496 从aspx转到chshtml还是有很多要适应的地方的,本帖是个人学习笔记帖不断更新.每天开 ...

  4. MVC 4.0 Razor模板引擎 @Html.RenderPartial 与 @Html.RenderAction 区别

    近来在学习MVC 4.0,设置布局全局网页的页脚,使用了Razor语法 @{Html.RenderPartial("Footer", Model.FooterData);} 但是并 ...

  5. ASP.NET Razor – C# 逻辑条件简介

    Razor 不是一种编程语言.它是服务器端的标记语言.C# 允许根据条件执行代码. If 条件 C# 允许根据条件执行代码. 使用 if 语句来判断条件.根据判断结果,if 语句返回 true 或者 ...

  6. ASP.NET Razor – 标记简介

    Razor 不是一种编程语言,它是服务器端的标记语言,Razor 是基于 ASP.NET 的,是为创建 Web 应用程序而设计的. 什么是 Razor? Razor 是一种标记语法,可以让您将基于服务 ...

  7. MVC5学习系列--Razor视图(一)

    前言 嗷~小弟我又出现了~咳咳..嚎过头了, 先说一说为什么写这个吧,~首先肯定是我自己需要学(废话 - -,)//,之前也写过MVC4的项目,嗯..但是仅限于使用并没有很深入的每个模块去了解, 这段 ...

  8. ASP .NET Core Web Razor Pages系列教程四:使用数据库进行交互 entity-framework(MySQL/MariaDB 版)

    系列文章目录:系列教程:使用ASP.NET Core创建Razor Pages Web应用程序 - zhangpeterx的博客 系列教程代码的GitHub地址:ASP .Net Core Razor ...

  9. ASP.NET Core Web Razor Pages系列教程八: 添加验证

    系列文章目录:系列教程:使用ASP.NET Core创建Razor Pages Web应用程序 - zhangpeterx的博客 系列教程代码的GitHub地址:ASP .Net Core Razor ...

最新文章

  1. NFV — Overview
  2. php 单元测试 麻烦,php – 正确的单元测试
  3. java EE map
  4. JAVA-重写equalse规范、技巧
  5. poj 2406 还是KMP的简单应用
  6. 【持久化框架】SpringMVC+Spring4+Mybatis3集成,开发简单Web项目+源码下载
  7. c# -- 动态生成查询lamda表达式
  8. androidStudio项目删除模块后报错解决办法
  9. 开多线程出现importerror_那些年让你迷惑的并行、并发、多线程、多进程、协程...
  10. Hello, cnblog!
  11. python2和3切换_python2和python3切换
  12. 第八章、面向对象设计
  13. 无法访问 请与这台计算机,我们办公室两台电脑想连接一个共享的打印机,但是连接的时候,总是显示无法访问,请与这台计算机的管理员联系,应该怎么设置啊...
  14. 《高质量C++/C编程指南(林锐)》学习笔记
  15. Win10 x64 VS2015 MFC打开主对话框报错:“未在此计算机上注册activex控件{648A5600-2C6E-101B-82B6-000000000014}”
  16. 小红书 标签 HTML5,牛宝-手机客户端
  17. excel冻结多行(固定表头)
  18. dos下查看shal值和Md5步骤
  19. 组合测试法是什么 软件测试,组合测试模型方法
  20. python魂斗罗源码_经典儿时游戏魂斗罗源代码

热门文章

  1. NLP《词汇表示方法(七)BERT》
  2. oracle中merge into用法解析
  3. Mac下安装caffe(cpu-only)
  4. leetcode - 638. 大礼包
  5. 常用sql操作语句实战演示
  6. 蔡高厅老师 - 高等数学阅读笔记 - 15 广义积分和伽马函数 定积分的应用(面积和体积) -(67、68、70、71)
  7. [笔记]前端 - 下拉菜单的实现
  8. PHP和MySQL的交互实验注意事项,PHP 和 MySQL 开发中要注意的8个技巧
  9. windows nginx 停止和启动_大数据离线项目实践之nginx服务器搭建
  10. python 操作word文字加粗、变红_使用python-docx在表格中使单元格加粗