语法使用剃刀(MVC 3)ASP.Net MVC菲尔哈克的直放站语法? [英] Syntax for ASP.Net MVC Phil Haack's Repeater syntax using Razor (MVC 3)?

查看:86
本文介绍了语法使用剃刀(MVC 3)ASP.Net MVC菲尔哈克的直放站语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ASP.Net MVC 3 RC 2我最近已经开始,并试图将现有的网站在MVC 2跨使用剃刀语法迁移。在MVC 2应用程序我使用的菲尔哈克在以下好心提供的code基转发:
菲尔哈克的$ C基于$ C中继

I have recently started using ASP.Net MVC 3 RC 2 and have attempted to migrate an existing website in MVC 2 across using the Razor syntax. In the MVC 2 application I am using the code base repeater that Phil Haack kindly provided in the following: Phil Haack's Code Based Repeater

我的问题是围绕着语法剃刀。我不明白如何在以下块模板可在剃刀被改写,也找不到任何文件来助阵(早期天的文档或我的简单...):

My question is around the syntax for Razor. I dont understand how the template in the following block can be rewritten in Razor and cannot find any documentation to help out (early days for documentation or my simplicity...):

<% Html.Repeater<ObjectToUse>(Model, "", "alt", (item, css) =>
   { %>
        <tr class="<%= item.Enabled ? css : "disabled" %>">
            <td><%= item.Name%></td>
            <td><%= item.Description%></td>
            <td><%= Html.RouteLink("Edit", item.ObjectToUseRouteValues("Edit"))%></td>
            <td></td>
            <td><%= Html.RouteLink("Select", item.ObjectToUseRouteValues())%></td>
        </tr>
<% }); %>

应用括号(对TR的)之间的模板时,问题就来了。我已经使用的WebGrid控制企图,但它不提供我需要设置禁用一行(我认为)的功能。

The problem comes when applying the template between the braces (the tr's). I have attempted using the WebGrid control, however it doesnt provide the functionality I require for setting a "disabled" row (I think).

推荐答案

其实,现在我想想更多一些,我不认为你可以使用动作参数像在剃刀。我记得运行到这之前。

Actually, now that I think about it some more I don't think you can use Action parameters like that in Razor. I recall running into this before.

从安德鲁护士回答:
不幸的是这是由设计在目前的解析器,但我要指出,我们希望在它得到改善。问题是标记只在一个声明开始有效的(这在技术上你到哪儿去把它),但是我们的C#解析器是不够聪明的时刻检测lambda表达式。

虽然这可能会过时:)

@Html.Repeater(Model, "row", "row-alt", 
    @<tr class="@item.ClassType : "disabled"">
        <td>@item.Name</td>
        <td>@item.Description</td>
        <td>@Html.RouteLink("Edit", item.ObjectToUseRouteValues("Edit"))</td>
        <td></td>
        <td>@Html.RouteLink("Select", item.ObjectToUseRouteValues())</td>
    </tr>
)

    public static IHtmlString Repeater<T>(this HtmlHelper html, IEnumerable<T> items,
         string className, string classNameAlt, Func<T, HelperResult> render) {
        if (items == null)
            return new HtmlString("");

        int i = 0;
        StringBuilder sb = new StringBuilder();
        foreach (var item in items) {
            item.ClassType = item.Enabled ? (i++ % 2 == 0 ? className : classNameAlt) : "disabled";
            sb.Append(render(item).ToHtmlString());
        }

        return new HtmlString(sb.ToString());
    }

}

这篇关于语法使用剃刀(MVC 3)ASP.Net MVC菲尔哈克的直放站语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆