0

Html.RenderActionのカスタムHtmlHelper拡張機能を作成しています。私の親ビューには、Html.Renderactionを呼び出すことによってレンダリングされる多くの異なる部分ビューが含まれます。ただし、管理者はロールの部分ビューの切り替えをソートするか、アプリケーション全体のアクションを完全に非アクティブ化することができます。そのため、Html.RenderActionの拡張メソッドを使用して、ロールをチェックし、ロールは特定のアクションにアクセスできます。この役割からアクションへのマッピングはxmlで行われ、メモリデータ構造の場合と同様にこのxmlを1回だけロードすることを計画しています。そして、htmlヘルパー拡張機能にそのデータ構造を調べてもらいます。それは良い方法ですか?より良い方法はありますか?

 @section column2 {
        @{Html.RenderActionIfIfAllowed("DashboardItem_Users", "DashBoard",User);}
        }

        @section column3 {
        @{Html.RenderActionIfIfAllowed("DashboardItem_Orders", "DashBoard", User);}
        }

上記の部分ビューをレンダリングする必要があります。だから私はHtml.RenderActionIfIfAllowedと呼ばれるhtmlヘルパー拡張機能を作成しました。

public static class HtmlHelperExtensions 
{
   public static void RenderActionIfIfAllowed<TModel>(this HtmlHelper<TModel> htmlHelper, string actionName, string controllerName, IPrincipal user)
    {
       //We can use the layour manager class to check if a particular role has access to an action and also if the action is active.
       //Hard coding here just for demo purpose
        if (user.IsInRole("Admin") && actionName != "DashboardItem_Users")
        {
            System.Web.Mvc.Html.ChildActionExtensions.RenderAction(htmlHelper, actionName, controllerName);
        }
        else if (user.Identity.IsAuthenticated && !user.IsInRole("Admin"))
        {
            System.Web.Mvc.Html.ChildActionExtensions.RenderAction(htmlHelper, actionName, controllerName);
        }
    }

}

このようにする理由は、ビューがアクティブであるかどうかに基づいて、ユーザーにaprtialビューを動的に表示するかどうかを指定するためです。ビューがユーザーに対してアクティブでないかどうかを示すxmlファイルを読み取り、それに応じてレンダリングします

4

1 に答える 1

0

私はこれのためにViewModelを作成し、boolプロパティを設定していました

public class DashBoardViewModel{

public DashBoard dashBoard{get;set;}

bool showItemDashBoard{get;set;}

bool showOrderDashBoard{get;set;}

}

コントローラーでは、ユーザーの役割を検証し、それらのブール値のプロパティを設定します。

ビューで

if(Model.showItemDashBoard){
  @Html.RenderAction("Action","Controller")
}
于 2013-02-26T05:29:12.977 に答える