1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Render MVC Views in pages with different templates

Render MVC Views in pages with different templates

3 posts, 0 answered
  1. Stuart
    Stuart avatar
    33 posts
    Registered:
    01 Nov 2012
    09 Jan 2013
    Link to this post

    Hi,

    Basically - I want to know if it's possible to render different views for an MVC widget into pages that are based on different templates.

    Long version: 
    I currently have an MVC widget with a single action - Index() - that renders out a list of items to the page as a horizontal scroller. The page that the widget is on consists of a number of such scrollers stacked on top of each other, and so there is very little vertical space allotted for each widget. I would like to add a details view - Show() - that will display more information about each of the items listed in scroller. The problem is that the default behavior of MVC widgets will try to put the resulting page from the Show() action into the short, wide spot allotted to the scroller. I have another template that we use for the rest of the site that is a much more standard layout; is it possible to have the Show() view render to a page using that template rather than to the page that the widget is located on with the highly-specialized horizontal scroller template?

    Thanks,
    Stuart

  2. Stoimen Stoimenov
    Stoimen Stoimenov avatar
    60 posts
    Registered:
    24 Aug 2017
    10 Jan 2013
    Link to this post
    Hi,

    Here is sample code how you can get page template for the current page:

    Guid currentPageId = SiteMapBase.GetCurrentNode().PageId;
    PageManager manager = PageManager.GetManager();
    PageData pageData = manager.GetPageData(currentPageId);
     
    var templateName = pageData.Template.Title;

    Just insert this in your Action and then by using the template name decide which view to return. I have created you a sample MVC widget that I hope will help you. It is just the basic MVC widget created with thunder and then I have added logic to detect current page template. You can simply unzip the files and add them to an empty blank Sitefinity project to test my solution. Regards,
    Stoimen Stoimenov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Stuart
    Stuart avatar
    33 posts
    Registered:
    01 Nov 2012
    15 Jan 2013 in reply to Stoimen Stoimenov
    Link to this post

    Hi Stoimen,

    Thanks for the example; however, that's not exactly the question I'm trying to answer. I'm not trying to pick a view to render depending on the template; I want to render the view to a page that uses a different template than the page the widget is included in.

    For instance: the widget is included on the homepage: example.com/, and the homepage uses the "HomepageTemplate". When an item in the Index() (default) view of the widget is clicked, I would like the Details() view to render to a *different page*, ie example.com/item_details, that uses a *different template*, ie "GenericSubpageTemplate", so that I can show the item details in a "normal" page. The default action for MVC widgets is to render the request view in the same content block in the same page in which the widget is included.

    The content block in which the widget is placed on the home page is very short, but quite wide. It works great for the index view that displays a horizontal list of items, but doesn't make sense for displaying detail - article text, publication information, etc. 

    Thanks,
    Stuart

3 posts, 0 answered