ASP.NET MVC Razor将不接受我的有效标记 [英] ASP.NET MVC Razor Won't Accept My Valid Markup
问题描述
我非常喜欢Razor语法,但是它肯定不够完美.例如,我有以下标记块.
I like the Razor syntax a lot, but it certainly falls short of perfect. For example, I have the following block of markup.
@if (Model.FeaturedDestinations != null && Model.FeaturedDestinations.Count() > 0)
{
int column = 0;
foreach (var d in Model.FeaturedDestinations)
{
column++;
if (column > 4)
{
</div>
@{ column = 1; }
}
if (column == 1)
{
@:<div class="row-fluid">
}
<div class="span3">
@RenderDestination(d)
</div>
}
</div>
}
因此,编辑器给了我弯曲的线条,指示我在开始的标签之前有一个结束的<div>
标签.我可以忍受这一点.但是,当我运行该应用程序时,实际上收到了以下运行时错误:
So, the editor gives me the squiggly lines indicating that I have an ending <div>
tag before an opening one. I can live with that. But when I run the app, I actually get the following run-time error:
遇到了结束标签"div",但没有匹配的开始标签.您的开始/结束标签是否正确平衡?
Encountered end tag "div" with no matching start tag. Are your start/end tags properly balanced?
很明显,我不能忍受!那么,处理这个案件有什么窍门吗?我知道我在做什么,只要我想要的标记.但是Razor并不这么认为,它正在接管.
Obviously, I cannot live with that! So is there any trick for dealing with this case? I know what I'm doing as far as the markup I want. But Razor doesn't think so and it's taking over.
为什么MVC浪费循环检查平衡标签?
And why the heck is MVC wasting cycles checking for balanced tags?
推荐答案
出于我不了解的原因,以下内容解决了该问题:
For reasons I don't understand, the following corrected the issue:
@if (Model.FeaturedDestinations != null && Model.FeaturedDestinations.Count() > 0)
{
int column = 0;
foreach (var d in Model.FeaturedDestinations)
{
column++;
if (column > 4)
{
@:</div>
column = 1;
}
if (column == 1)
{
@:<div class="row-fluid">
}
<div class="span3">
@RenderDestination(d)
</div>
}
@:</div>
}
请注意在多个标签之前添加了@:
.我不知道为什么这些是必要的-Razor突出显示它识别出这些是标签而不是代码.
Note the addition of @:
before several tags. I don't know why these are necessary--the Razor highlighting indicated that it recognized these were tags and not code.
此外,为什么这会使错误消失?导致运行时错误的原因没有改变.也许有人可以为我填补空白.
Also, why did this make the error go away? The thing that caused the run-time error has not changed. Perhaps someone can fill in the blanks for me.
这篇关于ASP.NET MVC Razor将不接受我的有效标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!