2

As I am new to Umbraco, I have not quite understood the routing mechanisms it seems to have.

I have a custom surface controller myProject.Controllers.CompanySurfaceController with a getCompanyList() and getCompany(int companyId) function.

The basic idea is to get the list of companies from the db, render the partial view with classic <a> links to the getCompany(..)function and retrieve/display that company from the db.

Everything is working fine except one thing: I cannot grasp how to create the <a> links to the child action of the controller! I have no problem including child actions in partial views when POSTing and using Html.Action.

I have tried @Html.ActionLink and other helpers but the closest I get to, is a link for /umbraco/Surface/CompanySurface/Company, which doesn't work of course and it does not include the id parameter (e.g. Company/3).

I have also tried to put the controllers in the umbraco/Surface namespace without luck (and it does not seem necessary).

What am I missing here?

dampee
  • 3,392
  • 1
  • 21
  • 37
lape
  • 41
  • 6

2 Answers2

1

First of all, I am wondering why you don't put the companies as nodes in your content tree by giving them an own document type. That would produce very simple code like this:

@foreach(var company in Model.Children) 
{
  <a href="@company.Url">@company.Name</a>
}

If the appoach above is not an option and you need to pull data out of an external (non umbraco) table, then do what you are doing. Except, you can not create a <a> to a child action! This is not something umbraco prohibits, this is ASP.Net MVC protection so users can't "hack" into your child actions. What your really want to use is a RenderMvcController (see documentation). There is another question digging into the difference between RenderMvcController and SurfaceController.

Community
  • 1
  • 1
dampee
  • 3,392
  • 1
  • 21
  • 37
  • Thanks for helping out! My application will probably end up having thousands of companies so it does not make sense to have a node per company even if it would give a really clean view. I do not understand the 'hack' you mention. MVC has standard routes as `{controller}/{action}/{id}` and isn't that a direct link? As I understand from the related question, Umbraco only has routing for content nodes (including custom controllers) but if `@html.action("myaction", "mycontroller", params)`can find its way, why am I then not able to link to /mycontroller/myaction/param? – lape Sep 17 '14 at 18:40
  • Do we really have to create custom routing code or extensions for simple non-umb data? – lape Sep 17 '14 at 18:41
  • No you don't. You could dump all code in your view. – dampee Sep 17 '14 at 22:58
  • did you ever find a solution for this? I have the exact same scenario and was using @HtmlActionLink with the same issue. I then was considering using a standard tag but i cant do that either the way i understand. Ive been bouncing between RenderMvcController and SurfaceController – Computer Aug 04 '22 at 15:43
  • an A tag would create a link to a node. The renderMvcController could kick in if it has the same name as the document type. SurfaceControllers are the equivalent of Html.Action and are meanly used for handling "form" submissions. I would first read about the difference between the two. None would work with a HtmlActionLink, only things like a "virtualNodeProvider" would work that way. But that is pretty advanced stuff which you probably don't need or want. Links are easily created by xxx – dampee Dec 01 '22 at 12:37
0

I ended up using the classic way of doing GET parameters instead. It works because i fetch all companies from the DB and handle everything in one controller (e.g. EditCompany). Then I can pass the /EditCompany?companyId=xx

It is not pretty but it is only needed for secure pages so I do not worry about SEO for now.

If we really need to make this work with custom controllers we need to implement a custom IContentFinder in Umbraco instead.

lape
  • 41
  • 6