tags:

views:

123

answers:

2

Hello.

I have a view in asp.net MVC that has two submit buttons. I would like to know which button was pressed. The buttons work GREAT so there is no issue there, I just need to do slightly different things depending on which button. I have checked the Request.Form[] collection and that doesn't contain anything.

Here is my view code....

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Data.TempPerson>" %>
<div class="phonePerson">
    <% using (Ajax.BeginForm("Create", new AjaxOptions
       {
           UpdateTargetId = "divList",
           HttpMethod = "Post",
           OnSuccess = "RedoLayout"
       }))
       { %>
    <label for="Name">
        Name:</label>
    <%= Html.TextBox("Name")%>
    <input type="submit" name="Button" id="Save" value="Save" class="btnSave" />
    <div id="phoneList" class="phoneList">
        <table>
            <% foreach (var item in Model.Phones)
               { %>
                 ... Stuff omitted for space ....
            <% } %>
            <tr>
                <td colspan="2">
                    <input type="submit" id="Add" name="Button" value="Add another phone" class="btn_AddPhone" />
                </td>
            </tr>
        </table>
        <% } %>
    </div>
</div>
+3  A: 

Two ways:

First, a string parameter to your "Create" function named "Button"

public ActionResult Create(string Button)//and other fields
{
if (Button == value1) then
    //do stuff
else if (Button == value2) then
   //do stuff
end if 
//return
}

Where value1 = "Add another phone"

If you are passing it in with the form collection, then it would be

if (formcollection["Button"] == value1)....

Russell Steen
+1 This is the right way to do it.
Evildonald
+3  A: 

I'd be tempted to add that I'd have used two forms, each one with a single submit button to ensure that each form only had a single responsibility. That would aid the separation of concerns and make the app more testable.

Lazarus
Very good point. Use of one global form does tend to carry over from ASP.net habits.
Russell Steen
Funny you should say that. :) I am migrating this from a solution that had two forms. The problem is that the forms' data in conjoined. The BEST solution would be to try and remove the second submit. :)
Craig
-1 however this doesn't allow you re-use controls on the same page. If you have a input used by both submits, you'll need to dupe that input.
Evildonald
Why would you want to reuse controls? The whole principle is that each 'form' has a single concern, if you are submitting one control through two 'actions' then you are not separating your concerns. I'd be interested in an example, especially given that you feel that my approach is wrong enough to warrant a down vote.
Lazarus
@Craig, how is the data conjoined?
Lazarus
In the worst way imaginable...bad design :) The phone numbers need the user's ID and the phone data is loaded from the user record. When a new phone is added, it appends to the user record and then the control is rerendered.
Craig
I'd be happier adding a second hidden userID control in the second form than trying to encompass both 'forms' within one.
Lazarus