Copied to clipboard

Flag this post as spam?

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


  • Stephen Turley 2 posts 22 karma points
    Feb 26, 2015 @ 00:09
    Stephen Turley
    0

    Passing id to a controller

    Hello, I'm quite new to Umbraco and I'm stuck on a basic requirement for which I cannot seem to get going as I would have expected. 

    I want to perform a basic get request for, example, /product/{action}/{id}. Which will hit the expected controller and action with an id and return a view with a populated custom model. I'll be using this link in a DataTables grid.

    My latest attempts have led to Umbraco failing to find a page 5 when I try /product/get/5 which sounded like a route problem.  Is there a complete example for me to follow so I can see where I have gone wrong? 

    I've picked up an example which got me going to a degree:

    http://shazwazza.com/post/Custom-MVC-routing-in-Umbraco

    But when I needed to pass a custom model that's when I get into a mess and my route didn't seem to work and I had to pass an id as a querystring in order for it to work.

    In a nutshell I have:

    • ProductController : RenderMvcController with public ActionResult GetProduct(int? id, ProductRenderModel model)
    • ProductController GetProduct return CurrentTemplate(model);
    • ConsentRenderModel : RenderModel with constructorpublic ConsentRenderModel() : base(new UmbracoHelper(UmbracoContext.Current).TypedContent(UmbracoContext.Current.PageId)) {}
    • CustomStartupHandler : IApplicationEventHandler which has a custom route for "Product/{action}/{id}
    • In Umbraco I have a Product document type, template and also a content/node page.  I've pulled up the physical view Product.cshtml and changed it to @inherits Umbraco.Web.Mvc.UmbracoViewPage<ProductRenderModel> and placed html to show properties of the model.

    Might be too much to ask but any help much appreciated.  An example would be good.

    Thank you.

     

     

     

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Feb 26, 2015 @ 12:07
    Jeroen Breuer
    0

    Hello,

    Have you tried passing in the value as a querystring? For example ?id=5. You can read the querystring data in your controller, but the model binder probably will also just work. 

    Looking at what you have in a nutshell you might want to look at the Hybrid Framework. It has a lot of tips and tricks for MVC in Umbraco.

    Jeroen

  • Stephen Turley 2 posts 22 karma points
    Feb 26, 2015 @ 20:09
    Stephen Turley
    0

    Hi, using a querystring was my work around but I feel like it is a hack as it should come via the route I would have thought.

    I have looked at the Hybrid Framework and for all it's complexity I haven't noticed or found any code that does what I want (please let me know if I've missed it),

    i.e. I would like a route to load up a record by id using a custom model and submit it for updates (these routes will be used in edit buttons on a search result grid).

    It seems many examples are blogging or news types which rely on editing to be performed by the CMS which is fair enough because that's what Umbraco is for but I want a few pages to be handled by our custom controllers because we have some custom tables.

    Thanks for replying.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Feb 26, 2015 @ 20:24
    Jeroen Breuer
    0

    You could try to use a Content Finder. With that you can match url's to nodes, but I'm not sure if you can pass the information to the controller.

    http://24days.in/umbraco/2014/urlprovider-and-contentfinder/

    Jeroen

Please Sign in or register to post replies

Write your reply to:

Draft