HTTPPost 不起作用 asp mvc 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 触发的
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可能需要在视图中的 Html.BeginForm() 中传递控制器和操作名称。由于 [HttpPost] Settings() 操作是针对 HTTP get 请求调用的,这意味着没有其他针对 get 请求的 Settings() 操作,因此我猜测您的视图是通过不同的操作提供的。在这种情况下,您需要在 Html.BeginForm() 中显式设置控制器和操作。试试这个:
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:
如果您希望发布帖子,则必须生成一个 html 表单,并将 method 属性设置为 post:
You have to generate a html form with the method attribute set to post if you want a post to happen:
应该有名称为 Index() 的操作,并且其中不应包含任何参数。这是我遇到的问题。
There should be action with name Index() and should not containg any parameters in it. This is the problem I have faced.
我已经使用 ActionName() 来解决同样的问题,
不工作代码:
工作代码:
I have used ActionName() to solve the same problem,
Not working code:
Working code:
剃须刀的正确使用方法
The proper way using razor