开发者

Does ASP.NET MVC swallow submit values?

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

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

However, this is not working for me. The values of all my buttons is null as non of them are contained within the Request.Form values. Is there a bug in ASP.NET MVC which swallows these values?

Here is my form code:

<% using (Html开发者_Go百科.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>

<% } %>

Here is my controller code:

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


From w3schools:

Important: If you use the button element in an HTML form, different browsers will submit different values. Internet Explorer will submit the text between the and tags, while other browsers will submit the content of the value attribute. Use the input element to create buttons in an HTML form.

It seems that this is not standardized. You should stick to

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


I would use inputs of type submit instead of buttons. Non-inputs may not passed back in a form post or at least can be passed back inconsistently. Note that they can have the same name with different values so that you can use the same parameter for any button that submits the form.

<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")
     {
        ...
     }

     ....
}


Using Firebug, I found that the submit buttons were not being sent in the response and because of that, there isn't much I can do on the MVC side. I decided to use a client side hack to populate a hidden input field on the client side which would be passed to the controller values.

I changed the input buttons to be:

<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" />

The jquery script simply copies the values:

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

The controller action then looks for the hidden input values:

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

This solves the issue from above but I realize it is not that clean.


Put them in two different forms and you will know which one submitted based on which action was called on the controller.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜