HtmlHelper in a controller

Last post 03-05-2008 12:52 PM by Orion2480. 3 replies.

Sort Posts:

  • HtmlHelper in a controller

    03-03-2008, 6:09 PM
    • Loading...
    • Orion2480
    • Joined on 02-13-2008, 11:00 AM
    • Posts 71

    Hi all.  I have a need for using Html.RenderUserControl() inside my controller.  Specifically, I am doing some AJAX calls whose response is small enough (such as a single form field) to not warrant a full RenderView().  Instead, I am doing HttpContext.Response.Write().  

    My problem is that I find myself duplicating some view code, in particular my custom controls.  These controls do not have any runat=servers in them, they're just a small collection of <%= %> to work on the data.  But since I don't have access to Html.RenderUserControl() from my controller actions, I'm having to duplicate the work.

    And, as well all know, duplicating work means the code lives in more than one place, which means it needs to be updated in more than one place if ever changed, which means it has bugs in more than one place if it has bugs, etc.  Not very DRY.

    Since AJAX calls so frequently work with *parts* of pages, it is not uncommon to return only part of a page.  Many of my views use custom controls to render them as "partials" to increase their reusability, and I would love to be able to use these controls from the controller for these small AJAX renderings.

    Anyway, does anyone have any suggestion about rendering my customer .ASCX controls from inside a controller?


     

    Filed under: , ,
  • Re: HtmlHelper in a controller

    03-04-2008, 1:55 PM
    • Loading...
    • Haacked
    • Joined on 09-17-2003, 10:43 AM
    • Posts 311

    If you call RenderView("NameOfUserControl") it will render the contents of the user control back to the response, rather than a full page. This assumes you're using a ViewUserControl in the normal Views/ControllerName directory.

    Phil Haack (http://haacked.com/)
    Senior Program Manager, Microsoft

    What wouldn’t you do for a Klondike bar?
  • Re: HtmlHelper in a controller

    03-05-2008, 11:15 AM
    • Loading...
    • Orion2480
    • Joined on 02-13-2008, 11:00 AM
    • Posts 71

     To test that, I just called the Action directly (instead of through AJAX).


    I did RenderView("NameOfControl.ascx");  but I get the following error:

    "Could not find an ASPX view at '~/Views/FolderName/NameOfControl.ascx.aspx'. "
     

  • Re: HtmlHelper in a controller

    03-05-2008, 12:52 PM
    Answer
    • Loading...
    • Orion2480
    • Joined on 02-13-2008, 11:00 AM
    • Posts 71

    I think I have a working solution based on what I found at http://www.brianclubb.com/2008/01/renderusercontr.html.  To quote from that page:

     

     

    public void GetStatistics(int id) 
    
    { 
    
       SeasonEntity season = new SeasonEntity(id); 
       HtmlHelper helper = new HtmlHelper(
                                 new ViewContext(this.ControllerContext, 
                                                 this.ViewData, 
                                                 this.TempData));
    
       string html = helper.RenderUserControl(
                           "/Views/PlaqueOrder/Statistic.ascx", season);
    
       Response.Write(html); 
    }
      
Page 1 of 1 (4 items)
Microsoft Communities
Page view counter