Copied to clipboard

Flag this post as spam?

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


  • Fahad Ayub 71 posts 238 karma points
    Apr 13, 2016 @ 14:53
    Fahad Ayub
    0

    SurfaceController

    Hi, 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

     public class ProductController : SurfaceController
    {
        // GET: Fragrance
    
            public ActionResult Index()
            {
    
                return View("Product");
            }
    
    }
    

    But when I run it I get error message HTPP 404 about view Product not found. The Umbraco engine for some reason looking in folder /umbraco/Surface/Product But all my views are under Views folder in my project. Please let me knpow how to fix this.

    Thanks

  • Steve Morgan 1348 posts 4457 karma points c-trib
    Apr 14, 2016 @ 08:05
    Steve Morgan
    0

    Hi Fahad,

    Welcome to Our - I think you've not had a reply as it's not clear what you're trying to do. Looking at your code I'm going to make an assumption or two.. apologies if I get this wrong. It looks like you've come from an pure MVC world and have gone head first in...

    @Html.ActionLink is not used in Umbraco - it depends what you're trying to do with this "Custom Controller" - but for generating links to your content you probably just want to use the in-built Umbraco goodness via Razor and the @Model object.

    There are two types of controllers in Umbraco - a Surface Controller (which you mention you want) and a Custom Controller which "route hijacks". You have't said what you're trying to do with this controller but from the name of it I'd guess it shouldn't be a SurfaceController but rather a custom controller but this is a guess :) .

    The basic rule of thumb is for forms use a Surface Controller for custom logic or where you want to use content from outside of Umbraco in an MVC way use a Custom Controller. The key point though is for smaller sites you would never need to write a Custom Controller (caveat - some developers prefer to always use these).

    You say you're new to Umbraco - have you created a site without using Controllers yet? E.g. following the basic tutorial https://our.umbraco.org/documentation/Tutorials/Creating-Basic-Site/

    A lot of pure MVC coders new to Umbraco dive straight in creatng custom controllers but I think it's key to get a handle of the Umbraco @Model and how the routing in Umbraco works first. Unless you have a site where lots of the data (e.g. products) are held externally or a requirement to have non-standard URLs IMHO it's usually best to let the Umbraco routing engine do the heavy lifting.

    If my assumption is wrong then I suspect you need to look at changing your ActionLink to Action and returning a PartialView (and put your views in the Views/Partials) - post what it is you're trying to do and I'm sure someone here will point you in the right direction.

    return PartialView("Product");
    

    Hope that makes sense

    Steve

  • Fahad Ayub 71 posts 238 karma points
    Apr 14, 2016 @ 09:37
    Fahad Ayub
    0

    Hi Steve,

    Thanks for your response. yes you are correct I am from pure MVC background. Actually our existing B2B website is written in PHP/MYQL and the website is at least 7 years old. So we are planning to rewrite the website completely so I thought I will give Umbraco a try. So for time being I am just learning and practicing few small steps. I know my question doesn't make much sense , the idea is to generate links (navigation) from database dynamically. Like you have different product categories then products then product types gender sizes etc. All this information will be stored in database. Then navigation/menu will be generated from the database so thats why I was thinking of using ActionLink to generate menu/Nav dynamically. When user clicks on a specific link the related controller will be invoked and then query database and then return model. So I assumes in this case I have to use custom controller.

    Thanks

  • Steve Morgan 1348 posts 4457 karma points c-trib
    Apr 14, 2016 @ 09:48
    Steve Morgan
    0

    Hi Fahad,

    Yes - then I think you need a Custom Controller with a custom route (so that everything is routed to it e.g. /products/{productid} ... etc

    https://our.umbraco.org/documentation/Reference/Routing/custom-routes

    Kind regards

    Steve

  • Fahad Ayub 71 posts 238 karma points
    Apr 14, 2016 @ 11:46
    Fahad Ayub
    0

    Hi Steve, Thanks for providing link to the doc. One more thing I would like to ask , using @Html.ActionLink right approach ?

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

    I tried to use the above code , but when the page renders the href property showing blank.

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

    Any idea?

    Thanks

  • Steve Morgan 1348 posts 4457 karma points c-trib
    Apr 14, 2016 @ 13:45
    Steve Morgan
    0

    ActionLink isn't valid in Umbraco - what URL are you expecting it to generate?

    You're fighting the Umbraco routing again - go with it :)

    Here's some links that can explain it better than I can.

    http://stackoverflow.com/questions/23520071/mvc-actionlink-in-the-umbraco-request-context

    http://stackoverflow.com/questions/18310483/umbraco-mvc-implementation-view-does-not-depend-on-umbraco-implementations

    Steve

  • Fahad Ayub 71 posts 238 karma points
    Apr 18, 2016 @ 10:30
    Fahad Ayub
    0

    Ok Thanks I will have a look in detail.

Please Sign in or register to post replies

Write your reply to:

Draft