部分不会被渲染

问题描述:

为什么在下面的剃刀的标记,当用户是经理部分不会被渲染

@foreach (var item in Model) { 
    <tr> 
     <td>@Html.DisplayFor(modelItem => item.IncidentId)</td> 
     ... 
     <td> 
      @Html.ActionLink("View", "View", new { id = item.IncidentId }) 
      @Html.ActionLink("Edit", "Edit", new { id = item.IncidentId }) 
      @{ 
      if (User.IsInRole("Manager")) 
      { 
       Html.ActionLink("Remove", "Remove", new {id = item.IncidentId}); 
      } 
      } 
     </td> 
    </tr> 
} 

事件(我可以证实,RoleProvider工作正常,因为在调试时我可以看到调试器进入代码调用Html.ActionLink为删除),实际的操作链接不会呈现到生成的HTML?

如果我只是把

@Html.ActionLink("Remove", "Remove", new { id = item.IncidentId }) 

它被渲染罚款。

@{ }是一个代码块,默认情况下,值不会写入响应输出。

你可以做两种:

@{ 
    if (User.IsInRole("Manager")) 
    { 
    @Html.ActionLink("Remove", "Remove", new {id = item.IncidentId}); 
    } 
} 

或:

@if (User.IsInRole("Manager")) 
{ 
    @Html.ActionLink("Remove", "Remove", new {id = item.IncidentId}); 
}