HTTPPost在ASP MVC 3中无法工作

3

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

 [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。

5个回答

2
您需要生成一个method属性设置为post的HTML表单,如果您想进行POST请求:
Html.BeginForm("action","controller", FormMethod.Post) { ... }

1
这也是我的第一个想法,但是无参数的 BeginForm 默认使用 FormMethod.Post。 - Rup

2

你可能需要在视图中的Html.BeginForm()中传递控制器和操作名称。由于[HttpPost] Settings()操作被用于HTTP GET请求,这意味着没有另一个Settings()操作用于GET请求,所以我猜测你的视图是从不同的操作中提供的。在这种情况下,你需要在Html.BeginForm()中明确设置控制器和操作。试试这个:

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

不,问题出在jquerymobile上,我用1.03版本修复了它。 - Khaldoun
1
@Khaldoun,你能分享一下你的答案吗?我也在使用JQueryMobile,并且遇到了类似的问题,在我的情况下,LogOn方法的RedirectToAction无法正常工作,地址栏会加上#号和重定向地址...感到困惑。 - Anand

0

我使用了ActionName()来解决相同的问题,

不起作用的代码:

[HttpGet]
    public ViewResult RsvpForm()
    {

    [HttpPost]
        public ViewResult RsvpFrom()
        {
        }

可运行的代码:

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

0

应该有一个名为Index()的动作,并且不应该包含任何参数。这是我面临的问题。


0

使用 Razor 的正确方式

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接