asp.net mvc 4从控制器按钮调用方法

时间:2022-12-01 23:20:44

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

在我的控制器中,我有类AccountController,并在我有这个方法

[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

在我的视图中,我有一个带有正文的cshtml页面和这部分代码

<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?

这不起作用,任何人都知道什么是问题或其他一些简单的解决方案?

3 个解决方案

#1


5  

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.

对于初学者,您不需要指定Controllers /,因为框架将为您找到控制器。实际上,客户端/ URL /等不知道控制器的“文件夹”的概念。您需要提供的是特定操作方法的“路线”。

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

由于MVC框架知道控制器的位置,因此您只需告诉它控制器上的控制器和操作方法:

action="Account/LogOff"

#2


5  

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:

action属性指向错误的控制器操作。您的控制器操作称为LogOff而不是AccountController。你永远不应该像这样手动构建

元素,但总是使用为此目的设计的html助手:

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

#3


0  

The form action should probably be /Account/LogOff

表单操作应该是/ 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:

尝试将其放在.cshtml文件中:

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

#1


5  

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.

对于初学者,您不需要指定Controllers /,因为框架将为您找到控制器。实际上,客户端/ URL /等不知道控制器的“文件夹”的概念。您需要提供的是特定操作方法的“路线”。

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

由于MVC框架知道控制器的位置,因此您只需告诉它控制器上的控制器和操作方法:

action="Account/LogOff"

#2


5  

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:

action属性指向错误的控制器操作。您的控制器操作称为LogOff而不是AccountController。你永远不应该像这样手动构建

元素,但总是使用为此目的设计的html助手:

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

#3


0  

The form action should probably be /Account/LogOff

表单操作应该是/ 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:

尝试将其放在.cshtml文件中:

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