user501579
user501579

Reputation: 251

Handling Multiple Submit Buttons

I Have a Strongly typed user control which i use for Searching a certain list of objects. the following code shows the user control,

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<PeercoreCRM.ViewModels.CustomerFilterViewModel>" %>
<div style="width: 100%;vertical-align:top;background-color:White">    
    <fieldset>
        <legend>Criteria</legend>
        <table cellspacing="0">
            <tr>
                <td style="width: 100px">
                    <div class="editor-label">
                        <%: Html.LabelFor(m => m.LeadName) %>
                    </div>
                </td>
                <td>
                    <div class="editor-field">
                        <%: Html.TextBoxFor(m => m.LeadName) %>
                    </div>
                </td>
            </tr>

            <tr>
                <td style="width: 60px">
                    <div class="editor-label">
                        <%: Html.LabelFor(m => m.CustomerCode) %>
                    </div>
                </td>
                <td>
                    <div class="editor-field">
                        <%: Html.TextBoxFor(m => m.CustomerCode)%>                         
                    </div>
                </td>
            </tr>                            

            <tr>
                <td>
                    <input type="submit" name="btnSearch" value="Search" />&nbsp;
                    <input type="submit" name="btnCancel" value="Cancel" />
                </td>
                <td>
                </td>
            </tr>
        </table>

    </fieldset>
</div>

In the View, I show this user control conditionally using the following code snippet,

<% using (Html.BeginForm("CustomerList", "Customer", new { isFiltered = Model.FilterViewModel.IsFiltered }, FormMethod.Post))
   {
    %>
<% if (Model.FilterViewModel.IsVisible) Html.RenderPartial("ListFilterUserControl", Model.FilterViewModel); %>
<% } %>

I have put the Form in the rendering page as this control is used in other views and thereby calling other action methods in different controllers.

I have the following method signature in my controller action method,

[HttpPost]
public ActionResult CustomerList(CustomerFilterViewModel filterModel)
{

    bool filtered = filterModel.IsDirty? FilterCustomers(filterModel):false;
    Session["CurrentPageNumber"] = null;

    return RedirectToAction("CustomerList", new { isFiltered = filtered || filterModel.IsFiltered });
}

My problem is, with this implementation, how can i separately identify which button is clicked ("Search" or "Cancel") and to write the code depending on that.

Upvotes: 2

Views: 3239

Answers (1)

mikelomaxxx14
mikelomaxxx14

Reputation: 143

To identify the button that has been passed in, you can group your buttons by adding a name attribute to them:

<input name="button" type="submit" value="Search" />&nbsp;
<input name="button" type="submit" value="Cancel" />

then, add a variable that is passed into your post method with the same name of the buttons (in this case "button") like this:

[HttpPost]
public ActionResult CustomerList(string button, CustomerFilterViewModel filterModel)
{
    if(button.Equals("Search")) 
    {
     bool filtered = filterModel.IsDirty? FilterCustomers(filterModel):false;
    Session["CurrentPageNumber"] = null;

    return RedirectToAction("CustomerList", new { isFiltered = filtered || filterModel.IsFiltered });
    } else {
       if(button.Equals("Cancel")) {
            //perform cancel
       }
    }
}

Upvotes: 8

Related Questions