Copied to clipboard

Flag this post as spam?

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


  • John Churchley 272 posts 1258 karma points c-trib
    Jun 02, 2015 @ 17:55
    John Churchley
    0

    Custom Controllers

    Hi,

    I'm trying to follow the following document on how to create a customer controller. 

    I'm currently stuck at the bit which says:

    Make your custom model inherit from Umbraco.Web.Models.RenderModel

    I assumed it meant something like this?

    namespace JEC.Web.Models
    {
        public class Product : Umbraco.Web.Models.RenderModel
        {
        }
    }

    however it's sugests Ithere is no constructor which takes no arguments i.e. needs to be 

    RenderModel(IPublishedContent content);

    or 

    RenderModel(IPublishedContent content, CultureInfo culture);

    https://our.umbraco.org/Documentation/Reference/Mvc/custom-controllers

  • Alex Skrypnyk 6131 posts 23950 karma points MVP 7x admin c-trib
    Jun 02, 2015 @ 18:31
    Alex Skrypnyk
    0

    Hi John,

    Can you show your view ? Can you inherit your views from UmbracoViewPage?

    @inherits UmbracoViewPage
    

    It solves problem for me.

    Thanks, Alex

  • John Churchley 272 posts 1258 karma points c-trib
    Jun 08, 2015 @ 12:16
    John Churchley
    0

    Hi Alex I want to inherit my custom Model but retain access to RenderModel properties like @Model.GetPropertyValue("bodyText") etc. The problem for me lies in the model not the view because it suggests that Umbraco.Web.Models.RenderModel doesn not contain a construction that takes 0 arguments.

  • John Churchley 272 posts 1258 karma points c-trib
    Jun 09, 2015 @ 16:20
    John Churchley
    0

    I'm trying to understand this point in the documentation

  • Casper Andersen 126 posts 508 karma points
    Jun 11, 2015 @ 08:53
    Casper Andersen
    0
    @inherits Umbraco.Web.Mvc.UmbracoViewPage<Your Model here>
    

    That should do the trick

  • Hywel Rees 56 posts 224 karma points
    Jun 11, 2015 @ 09:39
    Hywel Rees
    0

    Hi John,

    As you suspected, there are no parameterless constructors.

    The easiest way to achieve what (I think) you want, is to override the Index method in the controller, so:

    namespace JEC.Web.Models
    {
        public class Product : Umbraco.Web.Models.RenderModel
        {
            public override ActionResult Index(RenderModel renderModel)
            {
                ExampleModel myModel = new ExampeModel();
    
                // Perform your mappings here (or call a mapper)
                myModel.PageTitle = renderModel.GetPropertyValue<string>("pageTitle");
    
                // If you really want access to the IPublishedContent in your view, simply add it
                // to your strongly typed model
                myModel.Content = renderModel;
    
                // Then spit it back to your view
               return myModel;
            }
        }
    
        public class ExampleModel
        {
            public string PageTitle { get; set; }
            public IPublishedContent Content { get; set; }
        }
    }
    

    Now in your view, you simply add the model type, and query it to build up your page.

    @model JEC.Web.Models.ExampleModel
    
     <h1>@Model.PageTitle</h1>
     @* You can also query the IPublishedContent too if you wish *@
    
     <p>@Model.Content.GetPropertyValue<string>("welcomeMessage")</p>
    

    Hope this helps!

    Cheers,

    HJR

  • Hywel Rees 56 posts 224 karma points
    Jun 11, 2015 @ 09:42
    Hywel Rees
    0

    Well I couldn't work out how to edit my post :/

    There is a small correction:

    renderModel.GetPropertyValue
    // Should read...
    renderModel.Content.GetPropertyValue
    

    And

    myModel.Content = renderModel;
    // Should read...
    myModel.Content = renderModel.Content;
    
  • Hywel Rees 56 posts 224 karma points
    Jun 11, 2015 @ 09:46
    Hywel Rees
    0

    And the controller should extend RenderMvcController, not RenderModel - sorry, I think I've totally misunderstood your requirement here.

  • John Churchley 272 posts 1258 karma points c-trib
    Jun 12, 2015 @ 09:53
    John Churchley
    0

    Thanks Hywel I'll give that a try.

  • Hywel Rees 56 posts 224 karma points
    Jun 22, 2015 @ 18:32
    Hywel Rees
    1

    Hi John,

    If I could work out how to mark as a solution, I would :)

    Glad to have been a help.

    Cheers,

    Hywel

  • John Churchley 272 posts 1258 karma points c-trib
    Jun 23, 2015 @ 12:34
    John Churchley
    0

    Hi Hywel,

    It didn't work and tried your solution also but as there is a shared layouts page your method doesn't work either :(

    public class test : Umbraco.Web.Models.RenderModel
        {
            public int ID { get; set; }
        }
    
    
    
      public override ActionResult Index(test model)
        {
            model.ID = 1;
             return View(model);
        }
    

    Now with an error message no suitable method found to override.

  • Asbjørn 82 posts 195 karma points c-trib
    Jun 23, 2015 @ 13:09
    Asbjørn
    1

    If you want to inherit from RenderModel, you could create a constructor that takes an IPublishedContent and calls the base class constructor. Something like this:

    public class Product: Umbraco.Web.Models.RenderModel
    {
        public Product(IPublishedContent content)
            :  base(content)
        {
        }
    }
    

    That is how I usually do it. Depending on your use case, you may have to instatiate your model manually in your controller:

    var model = new Product(CurrentPage);
    

    This seems to work for me, but I am sure there are probably much smarter ways to do this.

  • John Churchley 272 posts 1258 karma points c-trib
    Jun 23, 2015 @ 13:35
    John Churchley
    0

    Hi Asbjørn, could you provide a more detailed example with controller?

    My attempt....

    Model

        public class Product: Umbraco.Web.Models.RenderModel
        {
            public Products(IPublishedContent content)
                :  base(content)
            {
            }
           public int ID
        }  
    

    Controller

     public override ActionResult Index(RenderModel model)
            {
                var model2 = new Products(CurrentPage);
      return base.Index(model2);
            }
    

    View

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<Products>
    
    @Model.ID
    
  • Asbjørn 82 posts 195 karma points c-trib
    Jun 25, 2015 @ 08:23
    Asbjørn
    1

    Sorry for not getting back to you sooner.

    Your example looks pretty good, except for the controller. If you are doing route hijacking, you would usually just call CurrentTemplate(), like this:

    public override ActionResult Index(RenderModel model)
    {
        var model2 = new Products(CurrentPage);
        return CurrentTemplate(model2);
    }
    

    You can also just call an MVC view in the standard MVC way, if you want to.

Please Sign in or register to post replies

Write your reply to:

Draft