Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

How to post to block controller action?

Ian
Ian
Vote:
 

I have the following form being rendered from within a block.

@using (Html.BeginForm("Logout", null, new { language = ContentLanguage.PreferredCulture.Name }, FormMethod.Post)) {
    @Html.AntiForgeryToken();
    <button type="submit">Log Out</button>
}

Here's the controller:

public class HeaderBlockController : BlockController<HeaderBlock>
{
    [HttpGet]
    public override ActionResult Index(HeaderBlock currentBlock)
    {
        var model = new HeaderBlockViewModel(currentBlock);
        return PartialView("Blocks/HeaderBlock", model);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Logout()
    {
        // Log user out

        // Redirect
        return Redirect("/");  
    }
}

When I submit the form, the page 404s and does not actually execute the Logout() method. What am I missing? Should I be approaching this differently?

#203513
Edited, Apr 25, 2019 22:24
Ian
Vote:
 

Thanks David, I stumbled across this and initially think it wasn't very helpful but it ultimately did solve my problem. Essentially, I needed to add virtual to the Logout() action result, which fixed the 404 issue. Hope this at least helps someone else in the future! Here's the correct method signature:

[HttpPost]
[ValidateAntiForgeryToken]
public virtual ActionResult Logout()
{
    // Log user out 

    // Redirect
    return Redirect("/");
}
#203515
Apr 25, 2019 22:43
Vote:
 

Great that you solved it and thanks for the information!

#203523
Apr 26, 2019 7:57
Vote:
 

is `virtual` related here with CastleProxies around block controllers pretending to be "normal" controllers? I haven't dig into this area yet..

#203530
Apr 26, 2019 11:09
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.