我有一个工作正常的代码块
@foreach (var message in Model.OrderBy(a => a.messageOrder))
{
if ((DateTime.Compare(message.messageStart, DateTime.Now) < 0) &&
(DateTime.Compare(message.messageEnd, DateTime.Now) > 0))
{
if (!message.hideHeader)
{
@Html.Raw(string.Format("<{0}>{1}</{0}>", message.headerClass,
(string.IsNullOrEmpty(message.headerHTML)) ? message.header :
Server.HtmlDecode(message.headerHTML)));
}
switch (message.numColumns)
{
case 1:
@Html.Partial("_contentBlock", message)
break;
default:
<div class="row">
@Html.Partial("_contentBlock", message)
</div>
break;
}
}
}
但是我尝试根据if / then来放置这个代码的div,并且不能使剃刀语法正确。在Visual Studio中,intellisence不会从if(!message.hideHeader)上突出显示。没有任何错误 -
@foreach (var message in Model.OrderBy(a => a.messageOrder))
{
if ((DateTime.Compare(message.messageStart, DateTime.Now) < 0) &&
(DateTime.Compare(message.messageEnd, DateTime.Now) > 0))
{
if (!string.IsNullOrEmpty(message.msgClass))
{
<div class="@message.msgClass">
}
if (!message.hideHeader)
{
@Html.Raw(string.Format("<{0}>{1}</{0}>", message.headerClass, (string.IsNullOrEmpty(message.headerHTML)) ? message.header : Server.HtmlDecode(message.headerHTML)));
}
switch (message.numColumns)
{
case 1:
@Html.Partial("_contentBlock", message)
break;
default:
<div class="row">
@Html.Partial("_contentBlock", message)
</div>
break;
}
if (!string.IsNullOrEmpty(message.msgClass))
{
</div>
}
}
}
任何想法都表示赞赏
答案 0 :(得分:0)
使用@:
if (!string.IsNullOrEmpty(message.msgClass))
{
@:<div class="@message.msgClass">
}
<!-- some other code goes here -->
@if (!string.IsNullOrEmpty(message.msgClass))
{
</div>
}
@:
告诉编译器它是C#代码块内HTML块的开头。