Note that there are some explanatory texts on larger screens.

plurals
  1. POHTTP POST from a ASP.NET MVC Ajax form does not include submit buttons
    text
    copied!<p>I am unable to determine which form submit button is clicked during an Ajax Form POST in ASP.NET MVC. I have a form that basically looks like:</p> <pre><code> &lt;% using (Ajax.BeginForm("Edit", new { code = Model.Code }, new AjaxOptions() { UpdateTargetId = "resultsDiv" })) {%&gt; &lt;p&gt; &lt;%= Html.TextBox("Name", Model.Name)%&gt; &lt;input id="submitButton1" name="submitAction" class="ajaxSubmitButton" type="submit" value="Button 1" /&gt; &lt;input id="submitButton2" name="submitAction" class="ajaxSubmitButton" type="submit" value="Button 2" /&gt; &lt;input id="testHiddenValue" name="testHiddenValue" type="hidden" value="hello world!" /&gt; &lt;/p&gt; &lt;% } %&gt; </code></pre> <p>After a standard HTTP POST (ie. JavaScript disabled), I get access to the following POST variables:</p> <ul> <li>Name = whatever</li> <li>submitAction = Button 1</li> <li>testHiddenValue = hello world!</li> </ul> <p>However, clicking that button with JavaScript enabled does not include the submitAction value. I have verified this by inspecting the POSTs with <a href="http://www.fiddler2.com/fiddler2/" rel="nofollow noreferrer">Fiddler</a>. </p> <p>My hunch is that the Microsoft Ajax library just doesn't serialize the values of the submit buttons. In any case, how can I get around this so my controller knows which button was clicked?</p> <hr> <p><strong>Edit:</strong> Yes, it looks like a bug in the Microsoft Ajax library (see below). To workaround this, I essentially added the following jQuery code:</p> <pre><code>$(document).ready(function() { $("#formId .ajaxSubmitButton").live("click", function() { $("#testHiddenValue").attr("value", $(this).attr("value")); }); }); </code></pre> <p>Then in my controller, if <code>Request.IsAjaxRequest() == true</code>, I can check the value of #testHiddenValue. Otherwise, I can look in <code>Request.Form["submitAction"]</code> and determine the correct course of action from there.</p> <p>Fairly clunky, but I can't really see an alternative.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload