如何在ASP.NET Core 2.0 MVC中的验证中放置一个@HTML帮助器

时间:2018-04-01 19:09:49

标签: asp.net-core-mvc

我想要做的是验证,如果用户是“用户”角色的成员,将呈现或不呈现两个动作链接。

到目前为止,这是我在谈论的部分而没有验证:

<td>
 <a asp-controller="Manage" asp-action="Edit" asp-route-id="@item.OwnerID">Edit</a> |
 <a asp-controller="Manage" asp-action="Delete" asp-route-id="@item.OwnerID">Delete</a> |
 @Html.ActionLink("Approve", "Approve", new { id = @item.OwnerID }, new { onclick = "return confirm('Are you sure you wish to approve this user?');" }) |
 @Html.ActionLink("Reject", "Reject", new { id = @item.OwnerID }, new { onclick = "return confirm('Are you sure you wish to reject this user?');" }) |
 <a asp-controller="Manage" asp-action="UpdateRole" asp-route-id="@item.OwnerID">Change Role</a>
</td>

这是我需要帮助的地方。

我想在以下代码中执行类似If(@item.RoleName =="User") )的操作:

<td>
  <a asp-controller="Manage" asp-action="Edit" asp-route-id="@item.OwnerID">Edit</a> |
  <a asp-controller="Manage" asp-action="Delete" asp-route-id="@item.OwnerID">Delete</a> |
**@if(item.RoleName == "User")**
{
   @Html.ActionLink("Approve", "Approve", new { id = @item.OwnerID }, new { onclick = "return confirm('Are you sure you wish to approve this user?');" }) |
   @Html.ActionLink("Reject", "Reject", new { id = @item.OwnerID }, new { onclick = "return confirm('Are you sure you wish to reject this user?');" }) |
**}**
<a asp-controller="Manage" asp-action="UpdateRole" asp-route-id="@item.OwnerID">Change Role</a>
</td>

编译器在第二个;之后期待@Html.ActionLink,当我转到视图时,我会在}之后得到无效的字词

我可以帮助你解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

它会将|作为按位OR运算符(您可以通过将其突出显示为代码的事实来判断)。在管道字符处手动切换回HTML模式:

@if (item.RoleName == "User")
{
    @Html.ActionLink("Approve", "Approve", ... ) @:|
    @Html.ActionLink("Reject", "Reject", ... ) @:|
}