Fahad
Fahad

Reputation: 173

Custom Controller Umbraco

I just started learning Umbraco, I have started small project just to learn Umbraco. I am having problem in creating custom controller. I have a link on my page for user to browse all the products:

@Html.ActionLink("View more", "Index", "Product", null, new { title = "Browse all Products" })

Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;

namespace Test.Controllers
{
    public class ProductController : Umbraco.Web.Mvc.RenderMvcController
    {
        public override ActionResult Index(RenderModel model)
        {
            return View("Products");
        }
    }
}

For some reason the web page show a the link with blank href

<a href="" title="Browse all Products">View more</a>

Upvotes: 1

Views: 1202

Answers (1)

elolos
elolos

Reputation: 4450

You don't necessarilly have to inherit from SurfaceController, as they are typically used for rendering MVC Child Actions and for handling form data submissions. In your case, a controller inheriting from Umbraco.Web.Mvc.RenderMvcController should be enough.

First check the Settings section of Umbraco and make sure that there is a Document Type called Product. Umbraco follows this convention for routing, so all pages of type Product will be routed to your ProductController. This routing convention might also help the HtmlHelper construct the action link correctly.

According to the documentation, the mapping works as follows:

  • Document Type name = controller name
  • Template name = action name
  • if no action matches or is not specified then the 'Index' action will be executed.

Finally, make sure that your controller action returns the template view. For example, if using the default RenderModel type, you can simply return the Template view for the document type:

public override ActionResult Index(RenderModel model)
{
    return base.Index(model);
}

Upvotes: 1

Related Questions