2012-02-16 195 views
1

爲什麼在下面的剃刀的標記,當用戶是經理部分不會被渲染

@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 }) 

它被渲染罰款。

回答

4

@{ }是一個代碼塊,默認情況下,值不會寫入響應輸出。

你可以做兩種:

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

或:

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