HTTP POST from ASP.NET MVC Ajax form does not include submit buttons

I cannot determine which form submit button was pressed during an Ajax POST form in ASP.NET MVC. I have a form that basically looks like:

    <% using (Ajax.BeginForm("Edit",
                             new { code = Model.Code },
                             new AjaxOptions() {
                                UpdateTargetId = "resultsDiv"
                             })) {%>
    <p>
       <%= Html.TextBox("Name", Model.Name)%>         

       <input id="submitButton1" name="submitAction" class="ajaxSubmitButton"
              type="submit" value="Button 1" />
       <input id="submitButton2" name="submitAction" class="ajaxSubmitButton" 
              type="submit" value="Button 2" />

       <input id="testHiddenValue" name="testHiddenValue" 
              type="hidden" value="hello world!" />
    </p>

<% } %>

After the standard HTTP POST (i.e. disabled JavaScript), I access the following POST variables:

  • Name = whatever
  • submitAction = Button 1
  • testHiddenValue = hello world!

However, clicking this button with JavaScript enabled does not include the submitAction value. I checked this by checking POST with Fiddler .

My guess is that the Microsoft Ajax library simply does not serialize the values ​​of submit buttons. In any case, how can I get around this so that my controller knows which button has been pressed?


: , Microsoft Ajax (. ). , jQuery:

$(document).ready(function() {           
    $("#formId .ajaxSubmitButton").live("click", function() {                
        $("#testHiddenValue").attr("value", $(this).attr("value"));
    });    
});

, Request.IsAjaxRequest() == true, #testHiddenValue. Request.Form["submitAction"] .

, .

+3
1

. . , . , , .

+2

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


All Articles