How Do I Render a View From a Different Controller

Last post 01-06-2008 9:17 PM by ghotiman. 5 replies.

Sort Posts:

  • How Do I Render a View From a Different Controller

    01-06-2008, 5:13 AM
    • Member
      205 point Member
    • ghotiman
    • Member since 08-28-2002, 5:36 AM
    • Missoula, MT
    • Posts 57

    I've built an RSS view that I would like to be able to render from a number of different controllers that provide different data.  By default RenderView() looks in the ~/Views/[ControllerName] folder for the view.  I can't figure out how to render this view from within different controllers.  I've heard that you are supposed to be able to override the default behavior for RenderView, but I'm not sure how to do that or if it is the right direction.  Any tips?  Thanks.

    Filed under: ,
  • Re: How Do I Render a View From a Different Controller

    01-06-2008, 10:44 AM
    • Participant
      983 point Participant
    • ChadThiele
    • Member since 04-27-2003, 2:43 AM
    • Japan
    • Posts 274

    Sounds like you want to make a shared view, as a user control. Then you can use the MvcToolkit to render the shared view, like so: 

    <%=Html.RenderUserControl("~/Views/Shared/RSS.ascx", RssData) %>

    Personally, I build separate RSS views for each controller... I usually end up tweaking them a bit so they're each somewhat different anyway. I don't really like the method I showed you above, it seems a bit like a hack. I mean, you'd have to create the RSS view for each controller anyway, then make a call to the RenderUserControl to render the shared view. Perhaps there will be a better, more baked in, method of doing this in the future.

    Did I answer your question(s)? Phweew...
  • Re: How Do I Render a View From a Different Controller

    01-06-2008, 11:14 AM
    Answer
    • Contributor
      4,372 point Contributor
    • tgmdbm
    • Member since 12-17-2007, 2:08 PM
    • Posts 883
    • ASPInsiders
      TrustedFriends-MVPs

     If you put your view in the Shared folder you can use

    RenderView( "RSS", rssData );

    from within your controller. 

  • Re: How Do I Render a View From a Different Controller

    01-06-2008, 3:22 PM
    • Member
      205 point Member
    • ghotiman
    • Member since 08-28-2002, 5:36 AM
    • Missoula, MT
    • Posts 57

    Thanks tgmdbm.  I thought I had already tried that, but I did it again and it works great!

  • Re: How Do I Render a View From a Different Controller

    01-06-2008, 7:45 PM
    • Participant
      983 point Participant
    • ChadThiele
    • Member since 04-27-2003, 2:43 AM
    • Japan
    • Posts 274

    tgmdbm:

     If you put your view in the Shared folder you can use

    RenderView( "RSS", rssData );

    from within your controller. 

     

    lol... I'd like to apologize to the original author. My reply was totally out of whack. Thanks, tgmdbm. 

    Did I answer your question(s)? Phweew...
  • Re: How Do I Render a View From a Different Controller

    01-06-2008, 9:17 PM
    • Member
      205 point Member
    • ghotiman
    • Member since 08-28-2002, 5:36 AM
    • Missoula, MT
    • Posts 57

    ChadThiele, I was actually considering something like you mentioned, and hoping that I wouldn't have to resort to it :)

Page 1 of 1 (6 items)