开发者

HTTPPost does not work asp mvc 3

开发者 https://www.devze.com 2023-02-15 23:19 出处:网络
I am really confused, here is the code : [HttpPost] public ActionResult Settings(string SubmitButton) { if (SubmitButton == \"Sign In\") {

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", "Accou开发者_如何学Cnt"); }
        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


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")) { %>


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) { ... }


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


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()
        {
        }


The proper way using razor

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

精彩评论

暂无评论...
验证码 换一张
取 消