title: MVC Authorization for partial views and elements
body: <p>MSDN says:</p>
  
  <blockquote>
    <p>To restrict access to an ASP.NET MVC view, you restrict access to the action method that renders the view. 
    To accomplish this, the MVC framework provides the AuthorizeAttribute class.</p>
  </blockquote>
  
  <p>I am working with an application that is loading nested Partial Views (for lack of a better term)</p>
  
  <pre><code>public ActionResult Index(int id)
  {
  ....stuff.....
  
  return PartialView("PartialViewName", model);
  
  }
  </code></pre>
  
  <p>In the above loaded Partial View is the nesting like:</p>
  
  <pre><code>PartialView.cshtml
  
  @Html.Partial("AnotherPartial", Model)
    -@Html.Partial("AnotherPartial_Partial", Model)
      -@Html.Partial("AnotherPartial_Partial_Partial", Model)
  @Html.Partial("YetAnotherPartial", Model)
  @Html.Partial("StillAnotherPartial", Model)
  </code></pre>
  
  <p>What do I do when I want to allow access to one Partial View but not another? What about specific elements in those partials? Like buttons, panels, divs, textboxes, etc...</p>
  
  <p>I have users and roles defined in my database, so I know who can access what element/partial view.</p>
  
  <p>Currently I am using my own Html Helper to show or hide partial views:</p>
  
  <pre><code>public static MvcHtmlString ShowHidePartial(this HtmlHelper helper, string   partialName, TheUser user)
      {
          bool? isVisible = false;
  
          //If I don't know who you are or what you are trying to view
          if (user == null || string.IsNullOrEmpty(partialName))
          {
              return MvcHtmlString.Empty;
          }
  
          if (IsAdmin(user))
          {
              return MvcHtmlString.Create(helper.Partial(partialName).ToString());
          }
          else
          {
              isVisible = IsVisible(partialName, user);
          }
  
          if (isVisible == true)
          {
              return MvcHtmlString.Create(helper.Partial(partialName).ToString());
          }
  
          return  MvcHtmlString.Empty;
      }
  </code></pre>
  
  <p>I'll probably stick with this method for rendering partials, but I am still unsure on how to proceed with restricting access/visibility to elements within these partials.</p>
  
  <p>For example, if the user has access to AnotherPartial.cshtml and there are 4 sections on that partial that allow the user to see/edit information (like member address, member status, member phone, and member date of birth) all with update/save buttons. These sections are exclusive to AnotherPartial.cshtml.</p>
  
  <p>Say I only want to allow the user to view (ReadOnly) 2 sections, edit (ReadWrite) 1 section, and completely hide the 4th section (Hidden?...No Access). How would I achieve that?</p>
  
  <p>I'm using Windows authentication for access to the application.</p>