asp.net mvc 4 calling method from controller by button

早过忘川 提交于 2019-12-05 06:15:32

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"

The action attribute is pointing to a wrong controller action. Your controller action is called LogOff and not AccountController. You should never be manually building <form> elements like that but always use the html helpers that are designed for this purpose:

@using (Html.BeginForm("LogOff", "Account"))
{
    <button class="btn btn-inverse" title="Log out" type="submit">Log Off</button>
}

The form action should probably be /Account/LogOff

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

Try putting this in the .cshtml file:

@using (Html.BeginForm("LogOff", "Account"))
{
    <button class="btn btn-inverse" title="Log out" type="submit">Log Off</button>
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!