Imagine being paid to improve your favorite CMS!

It could be you! Umbraco HQ are hiring and are looking for both talents and senior developers. Wonderful workplace, friendly colleagues and competitive salary.

Copied to clipboard

Flag this post as spam?

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


  • pat 119 posts 341 karma points
    Apr 19, 2017 @ 14:25
    pat
    1

    A public action method Index.cshtml was not found on controller

    Hi all , how to solve error public action method Index no found in controller

    I have added mvc view in to partials folder and then create a macro and call html action. I have link to the page and in page content I have added partial view macro file call Html.ActionI to index method on listSites Controller

    for all options page return error...

    tried adding [ChildActionOnly] then [HttpPost] then [HttpGet]

    System.Web.HttpException: A public action method '~/Views/Partials/ListSites/Index.cshtml' was not found on controller 'myicCodeProject.Controllers.ListSitesController'.

  • Jonathan Richards 209 posts 1219 karma points
    Apr 19, 2017 @ 14:39
    Jonathan Richards
    0

    Hi pat,

    Could you post the code of myicCodeProject.Controllers.ListSitesController

    But my money is on that you have made the class private, it needs to be public.

    Cheers

    Jonathan

  • pat 119 posts 341 karma points
    Apr 19, 2017 @ 14:44
    pat
    0
    namespace myicCodeProject.Controllers
    {
        public class ListSitesController : SurfaceController 
        {
            // GET: ListSites
    
            [HttpGet]
            public ActionResult Index()
            {
    
                if (Umbraco.MemberIsLoggedOn())
                {
                    TempData["test2"] = "member logged in";
                }
                var mysites = new List<SiteModel>();
    
                var s = new SiteModel();
                s.name = "ABC ltd";
                s.shortname = "Main";
                s.city = "Luton";
                s.postcode = "LL2 4GH";
                mysites.Add(s);
    
                var s1 = new SiteModel();
                s1.name = "JCob and Sons ltd";
                s1.shortname = "Main";
                s1.city = "Denver";
                s1.postcode = "DB2 4GB";
                mysites.Add(s1);
    
                var s2 = new SiteModel();
                s2.name = "Desi ltd";
                s2.shortname = "Logistics";
                s2.city = "Necastle";
                s2.postcode = "NW23 4FG";
                mysites.Add(s2);
    
    
    
                TempData["sitelist"] = mysites;
                ViewBag.sites = mysites;
               // return CurrentUmbracoPage();
                return View();
                // return View("~/Views/Partials/ListSites/Index.cshtml");
                 //return PartialView("~/Views/Partials/ListSites/Index.cshtml", new SiteModel());
            }
    
  • Jonathan Richards 209 posts 1219 karma points
    Apr 19, 2017 @ 15:01
    Jonathan Richards
    0

    Hi Pat,

    Well I lost my money there.

    Your SurfaceController looks good, I suppose you could post your razor code, but I'm wondering if you have a similar situation that catches me out sometimes, which is that my browser has cached that route as a 404 and won't change its mind until I kill all caches + I also feel more comfortable knowing that IIS (or IIS Express) has definitely restarted and added the route to its route table.

    Barring that, you have my sympathy. And I leave you to someone else with an eagle eye.

    Cheers

  • pat 119 posts 341 karma points
    Apr 19, 2017 @ 15:28
    pat
    0

    Yes you loose the money :)

    Thanks any way i'll try clear caches and see

    Pat

  • Damiaan 380 posts 1044 karma points c-trib
    Apr 19, 2017 @ 17:38
    Damiaan
    0

    How are you calling the @Html.Action(...)? Which parameters do you add?

  • pat 119 posts 341 karma points
    Apr 20, 2017 @ 09:20
    pat
    0

    I have tried

    @{Html.Action("~/Views/Partials/ListSites/Index.cshtml", "ListSites");}
    

    then

    @{Html.Action("~/Views/Partials/ListSites/Index.cshtml", "ListSites", new { method = "Get" });}
    

    I am calling this from Macro and using insert macro option in RTE ( not from template - if i do so its works fine list my sites)

    I have changed the method to Controllers name public class ListSites() and return PartialView

    now I don't get error about public method but still page is blank

     [HttpGet]
            public ActionResult ListSites()
            {
                if (Umbraco.MemberIsLoggedOn())
                {
                    TempData["test2"] = "member logged in";
                }
                var mysites = new List<SiteModel>();
    
                var s = new SiteModel();
                s.name = "ABC ltd";
                s.shortname = "Main";
                s.city = "Luton";
                s.postcode = "LL2 4GH";
                mysites.Add(s);
    
                var s1 = new SiteModel();
                s1.name = "JCob and Sons ltd";
                s1.shortname = "Main";
                s1.city = "Denver";
                s1.postcode = "DB2 4GB";
                mysites.Add(s1);
    
                var s2 = new SiteModel();
                s2.name = "Desi ltd";
                s2.shortname = "Logistics";
                s2.city = "Necastle";
                s2.postcode = "NW23 4FG";
                mysites.Add(s2);
    
    
    
                TempData["sitelist"] = mysites;
                ViewBag.sites = mysites;
    
             //   return PartialView("ListSites", new SiteModel());
                return PartialView("~/Views/Partials/ListSites.cshtml" ,new SiteModel());
            }
    

    then I changed @{Html.Action("ListSites", "ListSites");} no error but blank page :(

  • Damiaan 380 posts 1044 karma points c-trib
    Apr 20, 2017 @ 09:32
    Damiaan
    1

    Hi,

    Html Action can't call a partial. It can only call an Action on a controller. E.g:

    @Html.Action("ListSites", new {controller= "MyController"})
    

    If you want to call a partial (without a controller) then just call

    @Html.Partial("NameOfThePartial")
    @Html.Partial("~/Path/Of/Partial")
    
  • pat 119 posts 341 karma points
    Apr 20, 2017 @ 10:27
    pat
    0

    Sorry i am confused now.

    if i enter @{Html.Action("ListSites", new { controller = "ListSitesController"});} i get route error No route in the route table matches the supplied values

    if enter - page is blank @{Html.Partial("~/Views/Partials/ListSites.cshtml", new SiteModel());}

    Please do help me to sort out :)

  • pat 119 posts 341 karma points
    Apr 20, 2017 @ 10:41
    pat
    0

    Hi Damiaan,

    do you remond controller to return View () or Partialview ?

    I really need to figure out, why if I call html.action in Template that load my sites and not in macro ?

    What do i do wrong here ? i f I call in macro do i need to add [ChildActionOnly] what do i miss here ?

  • Damiaan 380 posts 1044 karma points c-trib
    Apr 20, 2017 @ 12:56
    Damiaan
    100

    Ok. You are mixing a few things here.

    Let first start with the syntax. A code block @{ ... } means running code, while @stuff means print out stuff. If you want to wrap Html.Action in a code block @{ ... }, you need to use @{Html.RenderAction(...)}, otherwise nothing will be printed out on your screen.

    To keep it simple, I always use @Html.Action(...) and @Html.Partial(...) I suggest you do the same. Except if you have a good reason not to of course.

    If you need to choose between View and PartialView, the PartialView is a good solution if you return partials (that is also childActions)

    Speaking of [ChildActionOnly]. If you are ONLY allowed to call a method on a controller from @Html.Action(), then you could add this attribute. It's not obligated though. It is only to prevent you are calling the method from the browser directly.

    To answer your questions: if the route is not found, it feels to me that the code is not compiled. And why macro is not printing the code: I guess that has to do with the codeblock vs @stuff thing.

    I hope this helps

    Kind regards

  • pat 119 posts 341 karma points
    Apr 21, 2017 @ 10:07
    pat
    0

    Spot on ... thank you very much. You save me

    I have changed Partial View macro call to Html.Action and remove {}

    all works fine. I owe you a coffee / tea and a chocolate , actually more than that.

    Thanks again for summarising each option above.....

  • Damiaan 380 posts 1044 karma points c-trib
    Apr 21, 2017 @ 12:29
    Damiaan
    0

    Glad it works!

    Find me on codegarden and let's have a coffee together! ;-)

    Kind regards
    Damiaan

Please Sign in or register to post replies

Write your reply to:

Draft