Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Asp.net templated razor delegates

887 views

Published on

Published in: Technology
  • Be the first to comment

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

×