Does ASP.NET MVC Provide Ingestion Data?

I have one form in ASP.NET MVC (v1) that has 2 input buttons. Each submit button should be contained in this single form, and I need to know which one the user clicked.

I know about this trick to check the FormCollection values ​​that will be returned based on the button clicked. For example, if I have and and the user presses the Button2 button, I should be able to say Request.Form ["Button2"]! = Null, and this will evaluate to true, in which case I know that the user clicked this button.

However, this does not work for me. The values ​​of all my buttons are zero, because they are not contained in the Request.Form values. Is there an error in ASP.NET MVC that swallows these values?

Here is my form code:

<% using (Html.BeginForm()) {%>

    <% Html.RenderPartial( "EditAreaControl", Model ); %>

    <div class="form-layout-command-container">
        <div class="form-layout-command-area-alpha"><button type="submit" name="submit1" value="Save">Save</button></div>
        <div class="form-layout-command-area-alpha"><button type="submit" name="submit2" value="SaveAndCopy">Save and Create Copy</button></div>
        <div class="form-layout-command-area-beta"><%= Html.ActionLink("Cancel", "list") %></div>
    </div>

<% } %>

:

[AcceptVerbs( HttpVerbs.Post )]
public ActionResult Add(FormCollection values )
{
   if (values["submit1"] != null)
        // always false
   if (values["submit2"] != null)
        // always false as well
}
+3
4

Firebug, , , - MVC . , , .

:

<input type="submit" value="Save" onclick="actions.copyValues($(this), $('#submitAction'));" />
<input type="submit" value="Save and Copy" onclick="actions.copyValues($(this), $('#submitAction'));" />
<input type="hidden" id="submitAction" name="submitAction" />

jquery script :

Actions.prototype.copyValues = function(from, to) {
            $(to).val($(from).val());
        };

:

var request = HttpContext.Request;
return request.Form["submitAction"];

, , .

+1

w3schools:

: HTML, . Internet Explorer , . HTML.

, .

<input type="submit" name="submitButton" value="Save" />
<input type="submit" name="submitButton" value="Cancel" />
+6

submit . , , . , , , .

<input type="submit" name="submitButton" value="Save" />
<input type="submit" name="submitButton" value="SaveAndCopy" />


public ActionResult Save( string submitButton, ... )
{
     if (submitButton == "Save")
     {
        ...
     }
     else if (submitButton == "SaveAndCopy")
     {
        ...
     }

     ....
}
+2

, , , .

0

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


All Articles