Copied to clipboard

Flag this post as spam?

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


  • John 18 posts 74 karma points c-trib
    Jul 02, 2015 @ 12:55
    John
    0

    Surface Controller index Method not Hit

    I am using a surface controller and it is working well for the forms. It even respects the authorize attribute I have applied to the controller (though this is due to child actions).

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = "_Layout.cshtml";
    }
    
    <h1>@Model.Content.Name</h1>
    

    Controller

    @Html.Action("MyForm", "MyController")
    
     [Authorize]
        public class MyController : SurfaceController
    {
    
        public ActionResult Index()
        {
            //Todo: Redirect Logic
            return CurrentUmbracoPage();
        }
    
        [ChildActionOnly]
        public ActionResult MyForm()
        {
            return View();
        }
    
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult FormSubmit(myModel model)
        {
             if (!ModelState.IsValid) 
                return CurrentUmbracoPage();
    
            return RedirectToUmbracoPage(1234)
        }
    
    }
    

    I now however wish to add another check and redirect the user accordingly. I cannot do this inside the child actions and need to do this on the main call to the page.

    I tried creating a method call index however it is never hit. I suspect the authorize attribute is only working as a child action is hit.

    My Controllers are in a different assembly to the main site but I do not think this is an issue.

    Any ideas?

  • Casper Andersen 126 posts 508 karma points
    Jul 02, 2015 @ 13:11
    Casper Andersen
    0

    Could you please post the code you use to call your index method of your controller ?

  • John 18 posts 74 karma points c-trib
    Jul 02, 2015 @ 13:14
    John
    0

    I am just using navigating to the URL of the published page that has the same name as the surface controller and the template.

    The code above is just sample code but it would be http://testdomain.com/My

  • Casper Andersen 126 posts 508 karma points
    Jul 02, 2015 @ 13:21
    Casper Andersen
    0

    Yeah you see, that will not work, Umbraco does not work that way, if you want to get hold of you index method you need to do this

    @Html.Action("ActionName (Index)", "ControllerName (MyController)")
    

    Where the things inside the parenthases are what your values should be.

    Hope it makes sense :)

    Casper

  • John 18 posts 74 karma points c-trib
    Jul 02, 2015 @ 13:26
    John
    0

    How can I redirect if some data is missing on a standard page or do I need to use an RenderMvcController instead.

    If use the RenderMvcController then I do not have access to the methods needed for the form post.

  • Casper Andersen 126 posts 508 karma points
    Jul 02, 2015 @ 13:33
    Casper Andersen
    0

    Depends on where you want to redirect to if you just want to redirect back to the same page, you can use

    return RedirectToCurrentUmbracoPage();
    

    and if you want to go to another Controller Method you can do

    return RedirectToAction("ActionName", "ControllerName");
    

    And to redirect to a different page you can use

    return RedirectToUmbracoPage(the pages id here);
    
  • John 18 posts 74 karma points c-trib
    Jul 02, 2015 @ 14:35
    John
    0

    What I mean is if I change to use the RenderMvc Controller RedirectToCurrentUmbracoPage and simmilar methods are not available.

  • Casper Andersen 126 posts 508 karma points
    Jul 02, 2015 @ 15:48
    Casper Andersen
    0

    That is true, but SurfaceController derives from RenderMVCController i believe

Please Sign in or register to post replies

Write your reply to:

Draft