奇怪的剃刀行为?

时间:2013-03-18 18:21:24

标签: asp.net html asp.net-mvc razor syntax

我在ASP.NET MVC项目的局部视图中有以下代码:

@if (Model.Url != null && Model.Url.Length > 0)
{ <a href="@Model.Url" target="_blank" title="Besök extern sida"><img src="~/Content/Theme/images/icons/link_13x13.jpg" alt="Extern länk" /></a>  }
else
{<img src="~/Content/Theme/images/icons/link_13x13_inactive.jpg" alt="Extern länk saknas" />}

这很棒!

但后来我明白了:

@if(!Model.PersoanlTagStatus.HasValue)
{ <div class="postContainer"> }
else if(Model.PersoanlTagStatus == ProjectX.Models.TagTypeKey.Ignore)
{ <div class="postContainer postConIgnore"> }
else if(Model.PersoanlTagStatus == ProjectX.Models.TagTypeKey.Favorite)
{ <div class="postContainer postConFav"> }

这根本不起作用?它抱怨它是否遗漏了}

为什么?

2 个答案:

答案 0 :(得分:2)

试试这个

@if(!Model.PersoanlTagStatus.HasValue)
{ 
   @: <div class="postContainer"> 
}
else if(Model.PersoanlTagStatus == ProjectX.Models.TagTypeKey.Ignore)
{ 

   @: <div class="postContainer postConIgnore"> 
}
else if(Model.PersoanlTagStatus == ProjectX.Models.TagTypeKey.Favorite)
{ 

   @: <div class="postContainer postConFav"> 
}

答案 1 :(得分:0)

使用此:

@{
    if(!Model.PersoanlTagStatus.HasValue)
    {
    @<div class="postContainer"></div> 
    }
    else if(Model.PersoanlTagStatus == ProjectX.Models.TagTypeKey.Ignore)
    {
    @<div class="postContainer postConIgnore"></div>
    } 
    else if(Model.PersoanlTagStatus == ProjectX.Models.TagTypeKey.Favorite)
    {
    @<div class="postContainer postConFav"></div> 
    }
}