+1-888-365-2779
Try Now
More in this section
Categories
Bloggers
Blogs RSS feed

How to display MVC widget in detail views of content items

by Stanislav Velikov
When placing MVC widgets on pages that also display blog posts, news items or dynamic content created with the module builder you may have noticed when blog post or any other content on a page when displayed in its details view doesn`t show the MVC widgets.

This is happening because when content is displayed in details view the url of the page appends the url for details view.

 For example if you open a page with this url http://mysite.com/page the MVC widget will render as the route where the MVC controller will be searching for action result will be the page route ( /page ).

When a news item among a list of news items is selected on this page and the whole news item appear the url becomes http://mysite.com/page/2012/12/15/latest-headlines.
On this url the MVC widget is gone as its controller can`t find the correct route to render MVC view.

To address this in the most appropriate way is to add UrlkeyPrefix for all content widgets on a page. Add the same prefix for blogs, news module builder widgets and other.


Add the same UrlkeyPrefix to each content widget on this page that will render details view. To render In the controller create a new action and put ActionName attribute to this method specifying the action name to the following : !{UrlKeyPrefix} (ex. [ActionName("!content")]).
Note: remember to put the exclamation mark before the key prefix ( ! )

In the controller class of the MVC widget add new action to render the controller view when the page currently displays details view of content item and the URL of the details view is http://mysite.com/page/content/2012/12/15/latest-headlines .
[Category("String Properties")]
        public string Message { get; set; }
 
        public ActionResult Index()
        {
            var model = new MyWidget1Model();
            if (string.IsNullOrEmpty(this.Message))
            {
                model.Message = "Hello, World!";
            }
            else
            {
                model.Message = this.Message;
            }
 
            return View("Default", model);
        }
 
[ActionName("!content")]
        public ActionResult Filter()
        {
            var model = new MyWidget1Model();
            if (string.IsNullOrEmpty(this.Message))
            {
                model.Message = "Hello, World!";
            }
            else
            {
                model.Message = this.Message;
            }
 
            return View("Default", model);
        }






8 comments

Leave a comment
  1. Scott Aug 04, 2013
    We are using this approach but are now finding that our search results and RSS feeds no longer work when linking in to the module detail pages because the URLs do not include the UrlKeyPrefix. Is there any way to globally specify that links to a page should include the UrlKeyPrefix, or otherwise can we set it somehow for the Search and RSS widgets?
  2. Jeff Jul 08, 2014

    @Scott, the following code allows you to use the same handler for any actions:

            public ActionResult Index()        {            var model = new MyWidget1Model();            if (string.IsNullOrEmpty(this.Message))            {                model.Message = "Hello, World!";            }            else            {                model.Message = this.Message;            }             return View("Default", model);        }

    protected override void HandleUnknownAction(string actionName) { Index().ExecuteResult(ControllerContext); }

  3. Mike B. Jul 10, 2014
    Thanks @Jeff, that seems like a much better solution than the solution provided in the blog post.
  4. Joe C Jun 03, 2015
    Thank you Jeff.  I am confirming that the solution Jeff provided worked for me as well.
  5. Saurav May 04, 2016

    @Jeff I used the solution provided by you but all the widgets (NewsList and slider) other than navigations and footer disappear  on calling the detailsview action method shown below. I applied breakpoint on handleunknownaction method and found out that it is not getting hit. Kindly, help.

    Here's my code of controller.

     [RelativeRoute("")]

    public ActionResult Index()

    {  

              string blogname = (string)Request.QueryString["name"];  

              ViewBag.blogname = blogname;    

             return View("BlogPosts"); 

    }

    [RelativeRoute("detailsview/{blogpostid}")]

    public ActionResult DetailsView(Guid blogpostid)    

    {        

             DetailViewModel model = new DetailViewModel();         

             BlogPost blogPost = GetBlogPost(blogpostid);       

             model.BlogPost = blogPost;        

             model.Userlike = dbManager.GetUserLikeStatus(blogpostid, user.UserId);   

             model.LikeCount = dbManager.GetLikeCount(blogpostid);     

             model.RelativeUrl = GetImageRelativeUrl(blogPost);    

            model.CommentsCount = GetCommentsCount(blogpostid);      

           

            return View("DetailsView", model);

    }

    protected override void HandleUnknownAction(string actionName) { Index().ExecuteResult(ControllerContext); }

  6. saurav May 04, 2016
    @stanislav I have created a custom feather MVC widget without a designer. In order to use your solution, I need to access the advanced properties of the custom widget. The problem is that I cannot find the advance button on the designer. A blank designer pops up with a save button. Please help.
  7. David Chelliah May 28, 2016

    Hey Saurav,

    If you would have designed the control using SimpleViewWidget Class, the mentioned property will be available. Since you have created a custom MVC widget, you need to use the method noted by Jeff.

    Writing a UnknowAction Handle - where you can select the Action to execute based on some value set in the HttpContext property or may be URL name

    You can see the related explanation about the MVC behaviour in the following link.

    http://davidsekar.com/sitefinity/mvc-widgets-disappear-in-detailed-view

    Hope this helps.

  8. David Chelliah May 28, 2016

    @Saurav:

    I saw your previous post & code in it. It looks like you need to handle this for the controls that actually disappers when the detail view is shown.

    NOT on the detailed view controller itself.

    Leave a comment