Razor Syntax HElp

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 年 前
Hi All,
in the HomepageProducts.cshtml file it's calling an extension method called Html.DataList which has a signature of:

public static IHtmlString DataList<T>(this HtmlHelper helper, IEnumerable<T> items, int columns,
            Func<T, HelperResult> template)
            where T : class
        {
}

@(Html.DataList<ProductModel>(Model.Products, 2,
            @<div class="item-box">
                @Html.Partial("_ProductBox", @item)
            </div>
                ))

just wondering if someone can explain how   @<div class="item-box">@Html.Partial("_ProductBox", @item)</div>

is a Func<T, HelperResult> ??

doesn't really make sense.. I'd love to ultimately strip the <div class="item-box"> out and just have
@(Html.DataList<ProductModel>(Model.Products, 2,
            @Html.Partial("_ProductBox", @item)
            ))


im not sure what the @ and markup does... i've seen @:

but not just @<div>

Thanks,
Tom
11 年 前
You should be able to strip out the [ <div class="item-box"> ... </div> ], shouldn't cause any issues. Have you tried it ? :)
11 年 前
yes.. I tried @(Html.DataList<ProductModel>(Model.Products, 2, @Html.Partial("_ProductBox", @item)))


and get Cannot Convert from MvcHtmlString to System.Func ... ... ...
11 年 前
Here is a Razor Syntax Quick Reference guide that I think we should all keep saved in our Bookmarks. I think the [ @ ] in front of the <div> tag is just to let Razor know that what follows is not C# code, but text/markup.
11 年 前
nice thank you for the guide.. but yes i have no idea why it is then not working if i remove the markup!
11 年 前
Try this:

Remove the un-needed markup, but instead wrap it with the Razor tags:

<text></text>
11 年 前
PERFECT! thanks so much! would still love an explanation of the Func keyword for that example!
11 年 前
Ah Excellent, and you're welcome :)

As far as I'm aware, the code:

Func<T, HelperResult>


I just think of it is just like a regular function, but short version. You give it something, and it gives you something back. In this case, you give it [ T ], and it returns a HelperResult object.

Check this out.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.