Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Hefin Jones 39 posts 161 karma points
    Aug 11, 2016 @ 15:26
    Hefin Jones
    0

    Custom Route, Controller and Model issue

    Hello :-)

    I'm trying to implement a "View Profile" page using a custom route, controller and model. But can't get it to work :-(

    My custom route is:

    RouteTable.Routes.MapRoute(
                    "memberProfileRoute",
                    "user/{profileURLtoCheck}",
                    new
                    {
                        Controller = "ViewProfile",
                        Action = "Index"
                    });
    

    My controller is:

    public class ViewProfileController : RenderMvcController
    {
        public ViewProfileController() : this(UmbracoContext.Current) { }
        public ViewProfileController(UmbracoContext umbracoContext) : base(umbracoContext) { }
    
        public override ActionResult Index(RenderModel model)
        {
            string profileURLtoCheck = Request.RequestContext.RouteData.Values["profileURLtoCheck"].ToString();
    
            ViewProfileViewModel profile = new ViewProfileViewModel();
    
            if (!String.IsNullOrEmpty(profileURLtoCheck))
            {
                var ms = UmbracoContext.Current.Application.Services.MemberService;
    
                var findMember = ms.GetAllMembers().FirstOrDefault(x => x.GetValue<string>("profileURL") == profileURLtoCheck);
    
                if (findMember != null)
                {
                    profile.Name = findMember.Name;
                }
            }
    
            return CurrentTemplate(profile);
        }
    }
    

    And the model is this:

    public class ViewProfileViewModel : RenderModel
    {
        public ViewProfileViewModel() : base(UmbracoContext.Current.PublishedContentRequest.PublishedContent) { }
    
        public string Name { get; set; }
    }
    

    This is my View:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<ViewProfileViewModel>
    @{
        Layout = "~/Views/Design.cshtml";
    }
    
    <h2>@Model.Name</h2>
    

    When I try to navigate to the route e.g. http://www.mysite.co.uk/user/joebloggs I keep getting errors. If I inherit from RenderModel in my model as shown above I get:

    Object reference not set to an instance of an object.

    on the constructor, and if I don't inherit from RenderModel I get the following error:

    Cannot bind source type ViewProfileViewModel to model type Umbraco.Web.Models.RenderModel

    I seem to be going around in circles .... any help would be greatly appreciated.

  • Micha Somers 134 posts 597 karma points
    Aug 11, 2016 @ 15:43
    Micha Somers
    0

    What happens if you add an IPublishedContent parameter to your constructor, like this:

    public class ViewProfileViewModel : RenderModel
    {
        public ViewProfileViewModel(IPublishedContent content) : base(content) { }
    
        public string Name { get; set; }
    }
    
  • Hefin Jones 39 posts 161 karma points
    Aug 11, 2016 @ 15:51
    Hefin Jones
    0

    Hi Micha,

    If I add the line of code to the Model as you suggested, I get an error in the Controller on this line:

    ViewProfileViewModel profile = new ViewProfileViewModel();
    

    There is no argument given that corresponds to the required formal parameter 'content' of 'ViewProfileViewModel.ViewProfileViewModel(IPublishedContent)

    If I update the controller line to:

    ViewProfileViewModel profile = new ViewProfileViewModel(CurrentPage);
    

    I get the following error:

    DataTokens must contain an 'umbraco-doc-request' key with a PublishedContentRequest object

    I assume this is because /user/profilename isn't an Umbraco page?

  • Micha Somers 134 posts 597 karma points
    Aug 11, 2016 @ 16:48
    Micha Somers
    1

    For creating an instance of your ViewProfileViewModel, I think you need to use model.Content instead of CurrentPage:

    ViewProfileViewModel profile = new ViewProfileViewModel(model.Content);
    
  • Hefin Jones 39 posts 161 karma points
    Aug 12, 2016 @ 08:08
    Hefin Jones
    0

    Still get an error, on that line :-( ...

    Object reference not set to an instance of an object.
    

    Isn't this because model.Content is null because my custom route / view isn't actually an Umbraco page?

  • Micha Somers 134 posts 597 karma points
    Aug 12, 2016 @ 09:40
    Micha Somers
    0

    So if I understand you correctly, there is no node available to refer to.

    For this approach you need an instance of IPublishedContent, so without a node being available, a virtual node must be created/found.

    The following resources can help you further on that topic:

    http://shazwazza.com/post/custom-mvc-routes-within-the-umbraco-pipeline/

    https://stackoverflow.com/questions/35647220/how-to-create-a-virtual-node-in-umbraco

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies