MVC3剃刀语法的烦恼 [英] MVC3 Razor Syntax troubles

查看:516
本文介绍了MVC3剃刀语法的烦恼的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让使用剃刀语法MVC3一个很简单的观点,但似乎我不能得到正确的语法。

我有一个简单的表像这样

 <表>
                &所述; TR>
                    @ {
                        变种计数器= 0;
                    }                    @foreach(在ViewBag.Categories VAR类)
                    {
                        反++;
                        &所述; TD>
                            <输入类型=复选框检查=选中NAME =@(类别+类别code)/>
                            @ category.Description
                        < / TD>                        如果(计数器%2 == 0)
                        {
                            < / TR>
                            &所述; TR>
                        }
                    }
                < / TR>
            < /表>

当我插入和if语句里面,我收到此错误


  

using块缺少结束}字符。


如果我试着来包装这两个标签里面,我得到这个错误,而不是:


  

在TR元素没有关闭。



解决方案

< / TR>< TR> 搅乱了HTML的流/ code组合。

您正在关闭在不同的级别,而不是不同程度HTML中的TR-标签,但code内。你应该欺骗剃须刀到输出HTML,它不分析自己。

您可以包括他们是这样的:

  @:LT; / TR>< TR>

  @ Html.Raw(< / TR>< TR>中)

结果:

 如果(计数器%2 == 0)
                        {
                            @:或其可/ TR>&下; TR>
                        }

点击为哈克的剃刀语法

I'm trying to make a very simple view using Razor syntax in MVC3, but it seems I can't get the syntax right.

I have a simple table like this

<table>
                <tr>
                    @{
                        var counter = 0;
                    }

                    @foreach (var category in ViewBag.Categories)
                    {
                        counter++;
                        <td>
                            <input type="checkbox" checked="checked" name="@("category" + category.Code)" />
                            @category.Description
                        </td>

                        if (counter % 2 == 0)
                        {
                            </tr>
                            <tr>
                        }
                    }
                </tr>
            </table>

When I insert the and inside the if-statement, I receive this error

The using block is missing a closing "}" character.

If I try to wrap those two tags inside and , I get this error instead:

The "tr" element was not closed.

解决方案

Your </tr><tr> messes up the "flow" of the html/code mix.

You are closing the tr-tag on a different level, not a different level in the html, but inside the code. You should trick razor into outputting html, that it does not parse itself.

You could include them like this:

@:</tr><tr>

or

@Html.Raw("</tr><tr>")

The result:

                        if (counter % 2 == 0)
                        {
                            @:</tr><tr>
                        }

Click for Haack's quick reference of Razor syntax

这篇关于MVC3剃刀语法的烦恼的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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