Copied to clipboard

Flag this post as spam?

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


  • Urvish 252 posts 776 karma points
    Apr 17, 2015 @ 15:59
    Urvish
    0

    How to get property value using Ditto in Controller without using MVC Render control

    Hi,

    I am using Umbraco 7.2.2.

    How to get property value using Ditto in Controller without using MVC Render control?

    I have made one class with document type property alias.

    As mentioned we are able to get property in View like below.

    var poco = Model.Content.As<MyTextPage>();
    
    @poco.Title
    

    But at the same time we are not able to get that value from the controller.

    Can any one help?

    Thanks in advance.

    Regards,

    Urvish Mandaliya

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Apr 17, 2015 @ 16:02
    Lee Kelleher
    0

    Hi Urvish,

    Sure, you can use Ditto the same way within your controller code...

    public override ActionResult Index(RenderModel model)
    {
        var poco = model.Content.As<MyTextPage>();
    
        // do something with your POCO
        // poco.Title
    
        return CurrentTemplate(poco);
    }
    

    Cheers,
    - Lee

  • Urvish 252 posts 776 karma points
    Apr 20, 2015 @ 07:18
    Urvish
    0

    Thanks Lee for the reply. And sorry for the late reply.

    My question is that.

    1) Is it compulsory to use (RenderModel model) as parameters?

    2) Can we have pre defined valued which we have filled in the content page in that var poco.

    For eg, I have created 3 property bodyText , title , image. And created one content page of that document type and filled all the value. So , can we get that values like poco.title , poco.bodyText , poco.image in the controller ?

    And also we need to create that properties in our MyTextPage class? or it will take automatic properties ?

    Regards,

    Urvish Mandaliya

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Apr 20, 2015 @ 08:24
    Lee Kelleher
    100

    Hi Urvish,

    1) Is it compulsory to use (RenderModel model) as parameters?

    It's not compulsory if you are using your own MVC controllers, (outside of Umbraco), but if you are making use of "route hijacking" then yes it will have the RenderModel parameter. See this documentation section for "Custom controllers" for more info:

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

    2) Can we have pre defined valued which we have filled in the content page in that var poco

    I'm not sure what you mean by "pre defined valued"? But with the POCO itself, yes you'll need to add those properties to your MyTextPage class, e.g.

    public class MyTextPage
    {
        public string Title { get; set; }
    
        public HtmlString BodyText { get; set; }
    
        public int Image { get; set; }
    }
    

    Note: With the Image property, by default it would be an int, but there are more advanced ways to map a media item to a POCO directly - but I think that's for a separate (follow-up) post.

    I hope this helps?

    Cheers,
    - Lee

  • Urvish 252 posts 776 karma points
    Apr 20, 2015 @ 08:34
    Urvish
    0

    Thanks Lee for the reply.

    Yes it helps a lot.

    "pre defined valued" means the values which I have entered in content page.

    enter image description here

    So can I get that value of title 'Test' and other value like this. poco.title = "Test" , poco.bodyText ="" etc.

    Regards,

    Urvish Mandaliya

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Apr 20, 2015 @ 08:38
    Lee Kelleher
    2

    Hi Urvish,

    Yes, absolutely - the values from the content node will populate the POCO properties. (That's the entire goal of Ditto!)

    Cheers,
    - Lee

  • Urvish 252 posts 776 karma points
    Apr 20, 2015 @ 08:42
    Urvish
    0

    Thanks a lot Lee for your support.

    I appreciate your help and support.

    I need to check from my self that what I am missing? Because I am not getting values from that poco properties.

    Thanks again.:)

    Regards,

    Urvish Mandaliya

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Apr 20, 2015 @ 08:53
    Lee Kelleher
    0

    I'd need to see some of your code if I can help to spot any issues.

    Maybe take a watch of the uHangout video I did about Ditto - here's a link to about 20 mins into the video where I show the demo:
    https://youtu.be/L40haIBLNS4?t=1040

    Cheers,
    - Lee

  • Urvish 252 posts 776 karma points
    Apr 20, 2015 @ 09:04
    Urvish
    0

    ok thanks Lee.

    Sure will let you know if I stuck up while implementing this.

    Regards,

    Urvish Mandaliya

  • Mehul Gajjar 48 posts 172 karma points
    Apr 23, 2015 @ 09:46
    Mehul Gajjar
    0

    Hi Lee,

    my question is how to use var poco = model.Content.As<MyTextPage>(); without using RenderModel and RendeMvcController

     public class myController : Controller   // <- this is possible or not? or always use RendeMvcController ?
     {
    
        public override ActionResult Index() // <- below code is possible without RenderModel ? 
        {
            var poco = model.Content.As<MyTextPage>();
    
    
    
            return CurrentTemplate(poco);
        }
    
    }
    

    Kindly advice on this

    Thanks in advance

    Regards,

    Mehul Gajjar.

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Apr 23, 2015 @ 09:54
    Lee Kelleher
    0

    Hi Mehul,

    This question isn't really about Ditto, it's about how to access content within your own MVC controller.

    Is there a reason why you can't inherit from UmbracoController?

    Thanks,
    - Lee

  • Mehul Gajjar 48 posts 172 karma points
    Apr 23, 2015 @ 14:42
    Mehul Gajjar
    0

    Hi Lee, Thanks for prompt reply ,

    i have now inherit control with RenderMvcController. and its working fine.

    but facing issue while i have set custom routing.

    Problem : Render model get null in controller via custom routing . see below.

    1) below is my star up handler

    public class MyStartupHandler : ApplicationEventHandler
    {
        protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            DefaultRenderMvcControllerResolver.Current.SetDefaultControllerType(typeof(umbNewsOverviewController));
    
            RouteTable.Routes.MapRoute(
             name: "TestNews",
              url: "TestNews/{*catchall}",
              defaults: new { controller = "umbNewsOverview", action = "Index" }
            );
    
            base.ApplicationStarting(umbracoApplication, applicationContext);
        }
    }
    

    2) controller

    public class umbNewsOverviewController : RenderMvcController
        {
           public override ActionResult Index(RenderModel model)
            {
    
                return base.Index(model);
    
            }
    
        }
    

    case 1) When i visit the page using this url http://localhost:63247/new i got value in RenderModel

    case 2) When i visit the page using this url http://localhost:63247/TestNews i got Null value in RenderModel

    please give some suggestion how to get value in RenderModel in case 2 using custom route.

    Thanks for your help.

    Regards

    Mehul.

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Apr 23, 2015 @ 14:57
    Lee Kelleher
    0

    Hi Mehul,

    With the given routes, what content are you expecting to get in "/TestNews"?

    Does this URL correspond with a node in Umbraco? If not, then you can use the UmbracoHelper (RenderMvcController has it as a property called Umbraco) ... so you can use any of the helper methods to get the node.

    e.g.

    var node = Umbraco.TypedById(1234);
    
  • Mehul Gajjar 48 posts 172 karma points
    Apr 24, 2015 @ 07:18
    Mehul Gajjar
    0

    Hi Lee ,

    Many Thanks for reply

    I will try with above

    Regards,

    Mehul.

Please Sign in or register to post replies

Write your reply to:

Draft