ASP.NET MVC
Templated Razor Delegates
Jason
Templated Razor Delegates
Index.cshtml
@{
Func<dynamic, object> strongTag = @<strong>@item</strong>;
}
<span>This sentence...
@Helper Syntax
Index.cshtml
@helper Create(string name, int age)
{
<p>
<span>@name</span>
<span>@age</span>
</p>
}
@Create...
@Helper Syntax
Person.cshtml in App_Code Folder
@helper Create(string name, int age)
{
<p>
<span>@name</span>
<span>@age</...
HelperResult Class
Index.cshtml
@{
var items = new[] { "one", "two", "three" };
}
<ul>
@items.List(@<li>@item</li>)
<ul>
HelperResult Class
RazorExtensions.cs
public static HelperResult List<T>(this IEnumerable<T> items,
Func<T, HelperResult> ...
Reference
 Templated Razor Delegates
 http://haacked.com/archive/2011/02/27/templated-razor-
delegates.aspx
 Helper syn...
Upcoming SlideShare
Loading in …5
×

Asp.net templated razor delegates

774 views
573 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
774
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
2
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Asp.net templated razor delegates

  1. 1. ASP.NET MVC Templated Razor Delegates Jason
  2. 2. Templated Razor Delegates Index.cshtml @{ Func<dynamic, object> strongTag = @<strong>@item</strong>; } <span>This sentence is @strongTag("Jason").</span>
  3. 3. @Helper Syntax Index.cshtml @helper Create(string name, int age) { <p> <span>@name</span> <span>@age</span> </p> } @Create("Jason", 18)
  4. 4. @Helper Syntax Person.cshtml in App_Code Folder @helper Create(string name, int age) { <p> <span>@name</span> <span>@age</span> </p> } Index.cshmtl @Person.Create("Jason", 18)
  5. 5. HelperResult Class Index.cshtml @{ var items = new[] { "one", "two", "three" }; } <ul> @items.List(@<li>@item</li>) <ul>
  6. 6. HelperResult Class RazorExtensions.cs public static HelperResult List<T>(this IEnumerable<T> items, Func<T, HelperResult> template) { return new HelperResult(writer => { foreach (var item in items) { template(item).WriteTo(writer); } }); }
  7. 7. Reference  Templated Razor Delegates  http://haacked.com/archive/2011/02/27/templated-razor- delegates.aspx  Helper syntax within Razor  http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3- and-the-helper-syntax-within-razor.aspx  MSDN-HelperResult  http://msdn.microsoft.com/zh- tw/library/system.web.webpages.helperresult(v=vs.111).aspx

×