【问题标题】:HTTPPost does not work asp mvc 3HTTPPost 不起作用 asp mvc 3
【发布时间】:2011-03-09 14:50:14
【问题描述】:

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

 [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 触发的

【问题讨论】:

    标签: c# asp.net-mvc-2 asp.net-mvc-3 http-post


    【解决方案1】:

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

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

    【讨论】:

    • 不是 jquerymobile 的问题,我用 1.03 版本修复了它
    • @Khaldoun 能否请您分享您的答案,我也在使用 JQUeryMobile 并正在努力解决类似的问题,在我的情况下,LogOn 方法的 RedirectToAction 不起作用并且地址栏填充有 # 符号和重定向地址...无知
    【解决方案2】:

    如果你想发帖,你必须生成一个带有设置为 post 的方法属性的 html 表单:

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

    【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      我用 ActionName() 解决了同样的问题,

      不工作的代码:

      [HttpGet]
          public ViewResult RsvpForm()
          {
      
          [HttpPost]
              public ViewResult RsvpFrom()
              {
              }
      

      工作代码:

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

      【讨论】:

        【解决方案5】:

        剃须刀的正确使用方法

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多