ASP.NET MVC Ajax.BeginForm eats the send button options. Looks like a mistake

If you use Ajax.BeginForm () with several submit buttons like this:

// View.aspx
<% using (Ajax.BeginForm("Action", "Controller", 
           new AjaxOptions { UpdateTargetId = "MyControl",  }))
{ %>
    <span id="MyControl">
       <% Html.RenderPartial("MyControl"); %>
    </span>
<% } %>

//MyControl.ascx
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<input name="prev" type="submit" value="prev" />
<input name="next" type="submit" value="next" />
//...

Everything is transmitted to the controller in order, but the parameters for the submit button that were pressed are not in the request. In other words, Request ["next"] and Request ["prev"] are always zero.

I looked at JavaScript in Microsoft.MvcAjax.js, and it looks like the Sys_Mvc_MvcHelpers $ _serializeForm function completely skips input like "submit".

This does not seem logical. How else can you find out which button was pressed?

This seems like a mistake. Is there a logical reason to skip these form parameters?

+2
source share
5

: 2009-11-21 MVC Release 2 Preview 2 , ​​ . MVC Release 2 Preview 1. , .

: 2009-08-07 MVC Release 2 Preview 1 , ​​ . script MicrosoftMvcAjax.debug.js, _serializeSubmitButton, , Ajax.BeginForm() , onclick, , " Microsoft JScript:" Sys.Mvc.AsyncForm " ".

, , , - . , , Ajax Forms , .

: 2009-05-07 Microsoft, , . , , .

, Microsoft. , , , , .

Ajax MVC. , , :

//===========
// View.aspx
//===========
<% using (Ajax.BeginForm("Action", "Controller", 
    new AjaxOptions { UpdateTargetId  = "MyControl", HttpMethod = "POST"}))
{ %>
    <span id="MyControl">
       <% Html.RenderPartial("MyControl"); %>
    </span>
<% } %>

//================
// MyControl.ascx
//================
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<input name="startIndex" type="hidden" value="0" />
<%= Ajax.ActionLink("Prev", "PrevAction",
    new AjaxOptions() { UpdateTargetId="MyControl", HttpMethod="POST"}) %>
<%= Ajax.ActionLink("Next", "NextAction", 
    new AjaxOptions() { UpdateTargetId="MyControl", HttpMethod="POST"}) %>
//...

:
, .

. 2 "Prev" "Next". "Prev" PrevAction , "startIndex" . "".

Actual:
, NONE , , POST.

- , ActionLink, . , URL- , POST.

JavaScript?
javascript, , , , . , -, , , , Internet Explorer.

, , , - Ajax.BeginForm() onsubmit(), . , onsubmit() , .

MicrosoftMvcAjax , , , . , WC3 . , . Internet explorer , , script .

( Firefox "explictOriginalTarget", , )

?
Microsoft . , - , , MicrosoftMvcAjax -. , mousedown, , onsubmit .

, , . , IE8, FireFox, MVC Ajax... . .

   <script type="text/javascript">
     var _clicked = "";
     function onSubmit(e) {
       var targ;
       if (!e) var e = window.event;
       if (e.target) targ = e.target;
       else if (e.srcElement) targ = e.srcElement;
       if (targ.nodeType == 3) //defeat Safari bug
           targ = targ.parentNode;
       alert("OnSubmit:" + _clicked + " was clicked.");
       return false;
     }
     function Click(e) {
       var targ;
       if (!e) var e = window.event;
       if (e.target) targ = e.target;
       else if (e.srcElement) targ = e.srcElement;
       if (targ.nodeType == 3) //defeat Safari bug
           targ = targ.parentNode;
       _clicked = targ.name;
       return true;
   }

   <form action="/Home/StandardForm" method="post" 
    onsubmit="onSubmit(event)" onmousedown="Click(event)">
      <input type="submit" name="StdPrev" value="StdPrev" />
      <input type="submit" name="StdNext" value="StdNext" />
   </form>
+4
+3

, MVC 2 ( ). , HTML . , .

Vote.aspx:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Vote</title>
</head>
<body>

<%using (Ajax.BeginForm("Vote", "Voting", new AjaxOptions { UpdateTargetId = "message" }))
  { %>
    <%= Html.Hidden("itemId", "1")%>
    <p>I love ASP.NET MVC!</p>

    <input type="submit" name="voteValue" value="+" />
    <input type="submit" name="voteValue" value="-" />
<%} %>

<p id="message"><%= TempData["message"] %></p>


<script type="text/javascript" src="<%= Url.Content("~/Scripts/MicrosoftAjax.js")%>"></script>
<script type="text/javascript" src="<%= Url.Content("~/Scripts/MicrosoftMvcAjax.js")%>"></script>

</body>
</html>

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

(8 2010) . HTML . . - ( , ), clicked.

+1

, . , .

0
source

I have done the following:

            <input id="btnSubmit" name="btnSubmit" type="hidden" value="" />
            <input type="submit" name="btnSubmit" value="Delete" id = "btnDelete"  onclick="$('#btnSubmit').attr('value','Delete');"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="submit" name="btnSubmit" value="Save New" id = "btnSaveNew"  onclick="$('#btnSubmit').attr('value','Save New');"/>
            <input type="submit" name="btnSubmit" value="Save" id = "btnSave" onclick="$('#btnSubmit').attr('value','Save');"/>

i.e. defined a hidden input type with the identifier "btnSubmit", and onclick event is added on each button, like onclick = "$ ('# btnSubmit"). attr ('value', 'Delete'); ". this seems to work since I was able to get the value of the button pressed in the controller:

public ActionResult SaveCreateBlot (line btnSubmit) {

}

0
source

Source: https://habr.com/ru/post/1718315/


All Articles