ASP.NET MVC剃刀 - 如果里面 [英] ASP.NET MVC Razor - if inside for

查看:149
本文介绍了ASP.NET MVC剃刀 - 如果里面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用输出剃刀HTML表格。表本身的工作,但现在我想一个名为甚至类添加到其他每个表中的行:

 <表>
    <&THEAD GT;
        &所述; TR>
            &所述;第i列1所述; /第i
            &所述;第i;柱2'/第i
            &所述;第i列第3版; /第i
        < / TR>
    < / THEAD>
    <&TBODY GT;
        @for(INT I = 0; I< ViewBag.MyList.Count;我++)
        {
            VAR myObject的= ViewBag.MyList [I]
            < TR @if(我%2 == 0){类=甚至}>
                < TD> myObject.Column1< / TD>
                < TD> myObject.Column2< / TD>
                < TD> myObject.Column3< / TD>
            < / TR>
        }
    < / TBODY>
< /表>

有显然有点问题的循环中,如果情况,但什么是写这个正确的方法是什么?


解决方案

 <表>
    <&THEAD GT;
        &所述; TR>
            &所述;第i列1所述; /第i
            &所述;第i;柱2'/第i
            &所述;第i列第3版; /第i
        < / TR>
    < / THEAD>
    <&TBODY GT;
        @for(INT I = 0; I< ViewBag.MyList.Count;我++)
        {
            VAR myObject的= ViewBag.MyList [I]
            < TR @ {如果(我%2 == 0){<文字和GT;类=甚至< /文字和GT; }}>
                < TD> myObject.Column1< / TD>
                < TD> myObject.Column2< / TD>
                < TD> myObject.Column3< / TD>
            < / TR>
        }
    < / TBODY>
< /表>

请看看通过菲尔哈克

剃刀语法快速参考

<一个href=\"http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx\">http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx

和斯科特guthries张贴关于您的问题

<一个href=\"http://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx\">http://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx

I'm trying to output an HTML table using razor. The table itself works, but now I'm trying to add a class called "even" to every other row in the table:

<table>
    <thead>
        <tr>
            <th>Column 1</th>
            <th>Column 2</th>
            <th>Column 3</th>
        </tr>
    </thead>
    <tbody>
        @for (int i = 0; i < ViewBag.MyList.Count; i++)
        {
            var myObject = ViewBag.MyList[i];
            <tr @if (i % 2 == 0 ) { class="even" }>
                <td>myObject.Column1</td>
                <td>myObject.Column2</td>
                <td>myObject.Column3</td>
            </tr>
        }
    </tbody>
</table>

There's obviously something wrong with that if-case inside the loop, but what is the correct way of writing this?

解决方案

<table>
    <thead>
        <tr>
            <th>Column 1</th>
            <th>Column 2</th>
            <th>Column 3</th>
        </tr>
    </thead>
    <tbody>
        @for (int i = 0; i < ViewBag.MyList.Count; i++)
        {
            var myObject = ViewBag.MyList[i];
            <tr @{if (i % 2 == 0 ) { <text>class="even"</text> }}>
                <td>myObject.Column1</td>
                <td>myObject.Column2</td>
                <td>myObject.Column3</td>
            </tr>
        }
    </tbody>
</table>

Please have a look at the razor syntax quick reference by phil haack

http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx

and scott guthries post about your problem

http://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx

这篇关于ASP.NET MVC剃刀 - 如果里面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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