如何使用辅助方法有条件地输出视图的一部分

时间:2013-09-26 12:54:40

标签: asp.net-mvc-4 razor

所以,假设我有一个视图的以下部分:

<td>
    @Html.ActionLink("Delete")
</td>

如果用户拥有特定权限,我只希望用户可以使用此权限 - DeleteItems。有两种方法可以用if语句包装链接,或者为需要一组权限的ActionLink助手重载。

If版本:

@if (User.HasPermission(Permissions.DeleteItems))
{
<td>
    @Html.ActionLink("Delete", "Delete")
</td>
}

重载版本:

<td>
    @Html.ActionLink("Delete", "Delete", Permissions.DeleteItems)
</td>

很酷 - 我认为这里显而易见的赢家是超负荷。

但是,如果我要阻止的区域更大,该怎么办?

<div class="deleteWrapper">
    <table>
        <tr>
            <td>Id</td>
            <td></td>
        </tr>
        @foreach (var item in items)
        {
            <tr>
                <td>@item.Id</td>
                <td>@Html.ActionLink("Delete", "Delete", new { id = @item.Id })
            </tr>
        }
    </table>
</div>

If版本当然可以使用,但我不希望在我的视图中到处都有@if (User.Has...(...))。我喜欢的是Html.BeginForm

的语法
@using(Html.BeginForm(...))
{
    ...
}

然而,BeginForm only adds to the response - 它实际上并没有对括号之间的视图做任何事情。有没有办法使用帮助器删除括号内的标记?

@using(Html.ShowIf(...))
{
    ...
}

1 个答案:

答案 0 :(得分:3)

是的,你可以。以下示例仅适用于Razor

@using (this.BeginPermissionScope(Permissions.DeleteItems))
{
    using (Html.BeginForm("Delete", "Home"))
    {
        <input type="submit" value="Submit" />
    }
}

C#代码:

public static class HtmlHelperPermissionScopeExtensions
{
    /// <summary>
    /// Begin new rendering scope based on current user permissions
    /// </summary>
    public static IDisposable BeginPermissionScope(this WebViewPage webViewPage, Permissions permission)
    {
        var currentIdentity = webViewPage.User.Identity;
        bool userHasPermission = currentIdentity.HasPermission(permission);
        return new PermissionScopeHelper(webViewPage, !userHasPermission);
    }

    /// <summary>
    /// Helper class that replaces current output stream with its own
    /// </summary>
    private sealed class PermissionScopeHelper : IDisposable
    {
        WebViewPage webViewPage;
        TextWriter oldWriter;
        TextWriter newWriter;

        /// <summary>
        /// Initializes helper class instance. If ignoreMarkupUntilDispose is set to true, then 
        /// all markup writen to response will be ignored
        /// </summary>
        public PermissionScopeHelper(WebViewPage page, bool ignoreMarkupUntilDispose)
        {
            if (ignoreMarkupUntilDispose)
            {
                webViewPage = page;
                newWriter = new StringWriter();

                // Replace output TextWriter for Write() and WriteLiteral() methods
                webViewPage.OutputStack.Push(newWriter);

                // Replace output TextWriter for all HtmlHelper and AjaxHelper extensions
                oldWriter = webViewPage.ViewContext.Writer;
                webViewPage.ViewContext.Writer = newWriter;
            }
        }

        /// <summary>
        /// Restore output TextWriters
        /// </summary>
        public void Dispose()
        {
            if (webViewPage != null)
            {
                webViewPage.ViewContext.Writer = oldWriter;
                webViewPage.OutputStack.Pop();
                newWriter.Dispose();
            }
        }
    }

}