Search code examples
asp.net-mvcbuttonasp.net-mvc-4controllers

asp.net mvc 4 calling method from controller by button


In my Controllers i have class AccountController and within in i have this method

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
    WebSecurity.Logout();
    return RedirectToAction("Index", "Home");
}

In my Views i have cshtml page with body and this part of code

<form class="float_left" action="Controllers/AccountController" method="post">
    <button class="btn btn-inverse" title="Log out" type="submit">Log   Off</button>
</form>

And this doesn't work, anyone know what is problem or some other simple solution?


Solution

  • You're not referencing the action method here:

    action="Controllers/AccountController"
    

    For starters, you don't need to specify Controllers/ because the framework will find the controller for you. Indeed, the notion of a "folder" of controllers isn't known to the client/URL/etc. What you need to give it is a "route" to the specific action method.

    Since the MVC framework knows where the controllers are, you need only tell it which controller and which action method on that controller:

    action="Account/LogOff"