ASP.NET MVC Ajax.BeginForm eats params of submit button clicked. Looks like bug

后端 未结 5 1553
粉色の甜心
粉色の甜心 2021-02-11 04:33

If you are using Ajax.BeginForm() with multiple submit buttons similar to this:

// View.aspx
<% using (Ajax.BeginForm(\"Action\", \"Controller\", 
                    


        
5条回答
  •  旧时难觅i
    2021-02-11 05:09

    I suppose this has been fixed in MVC 2 (or it was never broken). Just make sure your HTML markup validates. The following example should show it works.

    Vote.aspx:

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
    
    
    
    
    
        Vote
    
    
    
    <%using (Ajax.BeginForm("Vote", "Voting", new AjaxOptions { UpdateTargetId = "message" }))
      { %>
        <%= Html.Hidden("itemId", "1")%>
        

    I love ASP.NET MVC!

    <%} %>

    <%= TempData["message"] %>

    VotingController.aspx:

    using System.Web.Mvc;
    
    namespace Examples.FormWithMultipleSubmitButtons.Controllers
    {
        public class VotingController : Controller
        {
            public ViewResult Vote()
            {
                return View();
            }
    
            [HttpPost]
            public ActionResult Vote(int itemId, string voteValue)
            {
                switch(voteValue)
                {
                    case "+":
                        TempData["message"] = "You voted up.";
                        break;
                    case "-":
                        TempData["message"] = "You voted down.";
                        break;
                    default:
                        TempData["message"] = "Your vote was not recognized.";
                        break;
                }
    
                if(Request.IsAjaxRequest())
                {
                    return Content(TempData["message"].ToString());
                }
                else
                {
                    return View();
                }
            }
        }
    }
    

    I had the same issue today (Oct 8, 2010) with my form with multiple submit buttons. The HTML didn't validate. I cleaned it up. It's still doesn't validate (but less error than the original) and now the value of clicked button is submitted.

提交回复
热议问题