HTTPPost 不起作用 asp mvc 3

发布于 2024-10-20 22:34:31 字数 771 浏览 2 评论 0原文

我真的很困惑, 这是代码:

 [HttpPost]
    public ActionResult Settings(string SubmitButton)
    {
        if (SubmitButton == "Sign In") {
            ServiceLocator.Current.GetInstance<IAppContext>().LoggedUser = null;
            Response.Cookies["loginuser"].Expires = DateTime.Now;
            return RedirectToAction("Logon", "Account");
        }
        if (SubmitButton == "Sign Up") { return RedirectToAction("register", "Account"); }
        if (SubmitButton == "Change Default Ride Settings") { return RedirectToAction("changeSettings", "Home"); }
        return View();
    }

包含控制器的视图

<% using (Html.BeginForm()) {  %>

   Three input ,

<% } %>

不是用 httppost 触发的,而是用 httpget 触发的

I am really confused,
here is the code :

 [HttpPost]
    public ActionResult Settings(string SubmitButton)
    {
        if (SubmitButton == "Sign In") {
            ServiceLocator.Current.GetInstance<IAppContext>().LoggedUser = null;
            Response.Cookies["loginuser"].Expires = DateTime.Now;
            return RedirectToAction("Logon", "Account");
        }
        if (SubmitButton == "Sign Up") { return RedirectToAction("register", "Account"); }
        if (SubmitButton == "Change Default Ride Settings") { return RedirectToAction("changeSettings", "Home"); }
        return View();
    }

The view contain

<% using (Html.BeginForm()) {  %>

   Three input ,

<% } %>

the controller is not fired with httppost but fired with httpget

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忘羡 2024-10-27 22:34:31

您可能需要在视图中的 Html.BeginForm() 中传递控制器和操作名称。由于 [HttpPost] Settings() 操作是针对 HTTP get 请求调用的,这意味着没有其他针对 get 请求的 Settings() 操作,因此我猜测您的视图是通过不同的操作提供的。在这种情况下,您需要在 Html.BeginForm() 中显式设置控制器和操作。试试这个:

<% using (Html.BeginForm("Settings", "YourControllerName")) { %>

You probably need to pass in the controller and action names in Html.BeginForm() in your view. Since the [HttpPost] Settings() action is being invoked for HTTP get requests, that implies that there isn't another Settings() action for get requests, so I'm guessing that your view is being served from a different action. In such a case, you need to explicitly set the controller and action in your Html.BeginForm(). Try this:

<% using (Html.BeginForm("Settings", "YourControllerName")) { %>
烟雨凡馨 2024-10-27 22:34:31

如果您希望发布帖子,则必须生成一个 html 表单,并将 method 属性设置为 post:

Html.BeginForm("action","controller", FormMethod.Post) { ... }

You have to generate a html form with the method attribute set to post if you want a post to happen:

Html.BeginForm("action","controller", FormMethod.Post) { ... }
过气美图社 2024-10-27 22:34:31

应该有名称为 Index() 的操作,并且其中不应包含任何参数。这是我遇到的问题。

There should be action with name Index() and should not containg any parameters in it. This is the problem I have faced.

听你说爱我 2024-10-27 22:34:31

我已经使用 ActionName() 来解决同样的问题,

不工作代码:

[HttpGet]
    public ViewResult RsvpForm()
    {

    [HttpPost]
        public ViewResult RsvpFrom()
        {
        }

工作代码:

[HttpGet]
        public ViewResult RsvpForm()
        {
        }
        [HttpPost, ActionName("RsvpForm")]
        public ViewResult RsvpFromPost()
        {
        }

I have used ActionName() to solve the same problem,

Not working code:

[HttpGet]
    public ViewResult RsvpForm()
    {

    [HttpPost]
        public ViewResult RsvpFrom()
        {
        }

Working code:

[HttpGet]
        public ViewResult RsvpForm()
        {
        }
        [HttpPost, ActionName("RsvpForm")]
        public ViewResult RsvpFromPost()
        {
        }
嘿咻 2024-10-27 22:34:31

剃须刀的正确使用方法

@using (Html.BeginForm("LogOn", "Account", FormMethod.Post, new { id = "form1" }))
{
   //form content
}

The proper way using razor

@using (Html.BeginForm("LogOn", "Account", FormMethod.Post, new { id = "form1" }))
{
   //form content
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文