Inject Html Into a View Programmatically

Inject Html Into a View Programmatically

I have a tricky problem and I'm not sure where in the view rendering process to attempt this. I am building a simple blog/CMS in MVC and I would like to inject a some html (preferably a partial view) into the page if the user is logged in as an admin (and therefore has edit privileges).

I obviously could add render partials to master pages etc. But in my system master pages/views are the "templates" of the CMS and therefore should not contain CMS specific <% %> markup. I would like to hook in to some part of the rendering process and inject the html myself.

Does anyone have any idea how to do this in MVC? Where would be the best point, ViewPage, ViewEngine?



I need a language dictionary


Is this safe to store in custom IIdentity
You could use Html.RenderPartial to insert an HTML fragment any where in the page. ASP.NET MVC Routing - Pass inbound route value to outbound URLs automatically?If you want to insert it in a place not available to the view although only on the master you could place a <asp:ContentPlaceHolder ID="Admin" runat="server" /> placeholder inside the master and in the view simply override it and insert the partial. How easy is it to port ASP.NET MVC to Silverlight application and vice versa?If placing such a placeholder is not acceptable you could use AJAX like: $('#adminHolder').load('/home/admin');, although I would probably go with the previous approach as it will job in case the user has javascript disabled.. ASP.Net MVC Keeping parameter names and action argument names in sync
Null Values returned to Controller for Edit actionIs this the correct pattern to return different views from an ASP.NET MVC controller?


DataAnnotation attributes not working in Asp.Net MVC2
OK this took a bit of messing and the result is a little hacky. But it works and that's all this matters right.....
    protected override void Render(System.Web.UI.HtmlTextWriter writer)     {         if (!User.Identity.IsAuthenticated || !User.IsInRole("Admin"))         {             // If not admin continue as normal             base.Render(writer);             return;         }          // Taking a leaf out of the move viewstate to the bottom of page playbook         var stringWriter = new System.IO.StringWriter();         var htmlWriter = new HtmlTextWriter(stringWriter);         base.Render(htmlWriter);         var html = stringWriter.ToString();         var endOfBody = html.IndexOf("</body>") - 1;         if (endOfBody >= 0)         {             var adminConsole = Html.RenderPartialAsString("AdminPanel");             html = html.Insert(endOfBody, adminConsole);         }         writer.Write(html);     } 
I implement my own ViewPage overriding the Render method. This checks if the user is logged in as an admin and if they are, it renders a partial at the bottom of the page. Very similar to old skool viewstate hacks in webforms.. Enjoy..

53 out of 100 based on 58 user ratings 413 reviews